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?
You either need to override
EqualsinFoo, or implement anIEqualityComparer<Foo>so thatExceptcan tell when twoFoovalues are equal. For example:Then: