Here is example:
class A
{
public long ParentId;
public string ParentName;
public string Name;
}
// list is IEnumerable<A>
var selected = list
.Select(x => new {
parent = new {
id = x.ParentId,
name = x.ParentName
}
name = x.Name
});
var grouped = selected.GroupBy(x => x.parent);
So, as grouping completed successfully, I make conclusion, that parent is not created for two different entities if both of them have the same ParentId and ParentName. By other words, if list[i].ParentId == list[j].ParentId and list[i].ParentName == list[j].ParentName, then after selecting selected[i].parent == selected[j].parent.
Am I right? I thought, that new creates new object on every iteration through source collection. How .NET does this?
It’s a matter of how Equals is impemented. For anonymous classes it’s returns true if and only if the properties match and for each of the properties there is equality.
Compare these two: