I have a class with this structure:
public class BusinessObject
{
public int Column1 { get; set; }
public int Column2 { get; set; }
public string Column3 { get; set; }
public int Column4 { get; set; }
}
and I have a collection of objects of this type call BusinessObjectCollection. If I want an array of values that are distinct for only Column2… what must I do?
What if I dont know the property that I need… so if i have ColumnTitle as a String… and whatever the value of ColumnTitle… I want the distinct values for that property
Assuming you want the result to be an
IEnumerable<BusinessObject>, so thatColumn2is only used for implementing distinctness, there are two options:IEqualityComparer<BusinessObject>in a way which usesColumn2, and pass that intoDistinctUse
DistinctByfrom MoreLINQ:Obviously the second is simpler, but it does require an extra library (or copying just the code for
DistinctBy).