How to implement IEqualityComparer to return distinct values?

前端 未结 4 554
夕颜
夕颜 2020-12-01 06:15

I have a L2E query that returns some data that contains duplicate objects. I need to remove those duplicate objects. Basically I should assume that if their IDs are the same

4条回答
  •  离开以前
    2020-12-01 06:45

    An EqualityComparer is not the way to go - it can only filter your result set in memory eg:

    var objects = yourResults.ToEnumerable().Distinct(yourEqualityComparer);
    

    You can use the GroupBy method to group by IDs and the First method to let your database only retrieve a unique entry per ID eg:

    var objects = yourResults.GroupBy(o => o.Id).Select(g => g.First());
    

提交回复
热议问题