Use a delegate for the equality comparer for LINQ's Distinct()

后端 未结 4 802
梦毁少年i
梦毁少年i 2020-12-02 12:50

I have a LINQ Distinct() statement that uses my own custom comparer, like this:

class MyComparer : IEqualityComparer where T : MyType
{
            


        
4条回答
  •  孤街浪徒
    2020-12-02 13:26

    It's unfortunate that Distinct doesn't come up with such an overload, so what you have is a good option.

    With MoreLinq, you can use the DistinctBy operator.

    var distincts = bundle.GetAllThings.DistinctBy(a => a.Id); 
    

    You might also want to consider writing a generic ProjectionEqualityComparer that can turn the appropriate delegate into an IEqualityComparer implementation, such as the one listed here.

提交回复
热议问题