开发者

Get distinct or groupby list using linq of object property

I have

开发者_JAVA技巧
List<ObjA>  
ObjA has ObjB  
ObjB has property Id  

How do I get a list of distinct ObjB based on Id?

thanks


Using DistinctBy from MoreLinq, you can use this:

list.Select(a => a.ObjB).DistinctBy(b => b.Id);


You need to create your own IEqualityComparer<ObjB> to compare ObjBs using only their Id property. Then you can do:

class ObjBEqualityComparer : IEqualityComparer<ObjB> {
    public bool Equals(ObjB x, ObjB y) {
        return x.Id.Equals(y.Id);
    }

    public int GetHashCode(ObjB o) {
        return o.Id.GetHashCode();
    }
}

var objBComparer = new ObjBEqualityComparer();
var result = objAList.Select(o => o.ObjB).Distinct(objBComparer);


If you've overriden .Equals() on ObjB so that items with the same ID are considered equal (and items with different ID considered unequal), you can do

var list = GetListOfAs();

var distinctBs = list.Select(a => a.B).Distinct();

As Daniel Hilgarth noted in his answer, there is an extension library called MoreLINQ, in which there is a method called DistinctBy. With that, you don't need to override Equals at all - instead, just call

var distinctBs = list.Select(a => a.B).DistinctBy(b => b.ID);


List<ObjA> lst;
// ...
var distinctById = lst.Select(a => a.b) // get all B's
    .GroupBy(b => b.id)                 // group by id's
    .Select(g => g.First());            // take one object of each id


Here's a solution without the need of extensions:

List<ObjB> distinctObjB = lst
  .GroupBy(a => a.MyObjB.Id)
  .Select(b => b.First().MyObjB)
  .ToList();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜