Override EqualityComparer for LINQ to SQL class

I want to override the EqualityComparer for one of my LINQ to SQL classes (autogenerated by visual studio) so that when I look for a match with IndexOf, it will use my comparison instead of whatever it would do for this class. It does not seem to work, and my override does not get called.

Here is the logic for the comparison:

ClientFileList f = new ClientFileList();
f.ClientID = -1 * lines;
f.JobID = JobID;
f.DRSCode = aParts[0];
f.DocumentNumber = aParts[1];
f.Revision = aParts[2];
f.Title = aParts[3];
f.TitleLine2 = aParts[4];
f.TitleLine3 = aParts[5];
f.SheetNumber = aParts[6];
f.Status = aParts[7];
f.ColorCode = ColorCode;

if(oCFile.IndexOf(f)==-1)
{
    // this element is not a duplicate
    oCFile.Add(f);
}

and here is my extended class to override the EqualityComparer:

public partial class ClientFileList : EqualityComparer<ClientFileList>
{
    public override bool Equals(ClientFileList x, ClientFileList y)
    {
        //throw new NotImplementedException();
        return (x.DocumentNumber == y.DocumentNumber &&
                x.Revision == y.Revision);
    }

    public override int GetHashCode(ClientFileList obj)
    {
        String s = obj.DocumentNumber + obj.Revision;
        return s.GetHashCode();
        //throw new NotImplementedException();
    }
}

What I am hoping for is if a document with matching DocumentNumber and Revision is already in the database, I don't want to add it again (this is implemented while reading a delimited text file and inserting into the database). MSDN documentation on IndexOf seemed to indicate that it would use the EqualityComparer . . . but I am not seeing that behavior. What am I doing wrong?

Jon Skeet
people
quotationmark

I want to override the EqualityComparer for one of my LINQ to SQL classes

I really don't think you do. I think you want to override the Equals method, and probably implement IEquatable<T>. EqualityComparer is meant for an object which is able to compare objects of other types - for example an AgeComparer might derive from EqualityComparer<Person>.

If you override Equals(object) and GetHashCode, and also implement IEquatable<T>, then I'd expect IndexOf to work, assuming that oCFile is a List<ClientFileList> or something similar (you haven't told us). However, it won't affect how LINQ to SQL itself issues queries, of course.

people

See more on this question at Stackoverflow