Get distinct or groupby list using linq of object property?

Using DistinctBy from MoreLinq you can use this: list. Select(a => a. ObjB).

DistinctBy(b => b. Id).

Using DistinctBy from MoreLinq, you can use this: list. Select(a => a. ObjB).

DistinctBy(b => b. Id).

Impressive, thank you very much for the linq to that webpage ;P – Valamas Jul 27 at 11:34.

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 lgarth 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).

You need to create your own IEqualityComparer to compare ObjBs using only their Id property. Then you can do: class ObjBEqualityComparer : IEqualityComparer { 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).

List 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 distinctObjB = lst . GroupBy(a => a.MyObjB. Id) .

Select(b => b.First(). MyObjB) .ToList().

Var distinctBs = list. Select(a => a. Var distinctBs = list.

Select(a => a.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions