LINQ Except/Distinct based on few columns only, to not add duplicates

Excuse me for the confusing title. But I couldn't come up with something more simpler.

I have got below class.

public class Foo
{
    public FooId int { get; set; }

    public FooField string { get; set; }

    //some more fields here....
    //....
    //....

    public DateTime CreatedDate { get; set; }
}

I need to loop through something and add a range of Foo to a List<Foo>, but it should not be duplicate based on the combination of FooId and FooField.

So I am trying as below

List<Foo> foos = new List<Foo>();

foreach (Bar item in blah.Bars)
{
    //Some code here to get the foos from the item
    List<Foo> processedFoos = item.GetFoos();

    //The problem is with below line
    foos.AddRange(processedFoos.Except(foos));
}

The Except adds all the records and duplicates the FooId and FooField combination as CreatedDate would be unique for all records.

But I need to ignore the CreatedDate and just add those records which does not violate the unique combination.

What can I do here? Except? Distinct? Any other alternative?

Most important, how?

Jon Skeet
people
quotationmark

You either need to override Equals in Foo, or implement an IEqualityComparer<Foo> so that Except can tell when two Foo values are equal. For example:

public sealed class FooComparer : IEqualityComparer<Foo>
{
    public bool Equals(Foo x, Foo y)
    {
        return x.FooId == y.FooId && x.FooField == y.FooField;
    }

    public int GetHashCode(Foo foo)
    {
        // Note that if FooField can be null, you'll need to account for that...
        return foo.FooId ^ foo.FooField.GetHashCode();
    }
}

Then:

 foos.AddRange(processedFoos.Except(foos, new FooComparer()));

people

See more on this question at Stackoverflow