I have two objects: A containing a list of B objects :
I call a method which returns a list of B elements. Then I want to group these B elements by their A property and in the end get a list of A objects :
var groupedByA = bList.GroupBy(b => b.A).Select(a => a.Key).ToList()
That works fine, but in my case, the BList of these A objects are not set, so I also want to associate the B list of the groupBy instruction to A, something like
var groupedByA = bList.GroupBy(b => b.A).Select(a => a.Key then a.Key.BList = a.Val).ToList()
Any idea how can I do that?
You could write an statement lambda:
.Select(group => {
var bList = group.ToList();
var a = group.Key;
a.BList = bList;
return a;
})
You may want to add some comments or otherwise note that the objects will be modified. Normally you would expect LINQ statements to be side-effect free.