Grouping a list of of entities which has a sublist in c#
Posted
by mikei
on Stack Overflow
See other posts from Stack Overflow
or by mikei
Published on 2009-12-23T10:18:30Z
Indexed on
2010/04/28
3:43 UTC
Read the original article
Hit count: 324
I have a set of Entities which basically has this structure.
{Stats Name="<Product name> (en)" TotalResources="10" ..}
{DayStats Date="2009-12-10" TotalResources="5"}
{DayStats Date="2009-12-11" TotalResources="5"}
{Stats}
{Stats Name="<Product name> (us)" TotalResources="10" ..}
{DayStats Date="2009-12-10" TotalResources="5"}
{DayStats Date="2009-12-11" TotalResources="5"}
{Stats}
...
What I wan't to extract from this set is a new set entities (or entity in the example above) where the first level has been grouped by the (ignoring the country label) where all/some of the properties has been summmed together, including the the sublist of {DayStas} on a per day basis. So the result set of the example would look something like this:
{Stats Name="<Product name>" TotalResources="20" ..}
{DayStats Date="2009-12-10" TotalResources="10"}
{DayStats Date="2009-12-11" TotalResources="10"}
{Stats}
...
So my question is: Is it possible to do this in a more elegant way in LINQ rather than the vanilla "loop-trhough-each-entity-and-compare"-way?
And I wan't the set to contain the same type of entities as the original set ({Stats}, {DayStats}), your answer doesn't need to include code for that, I can probably work that out by myself. Just letting you know incase you need to take that into account.
(I'm sorry if this question has been discussed before, I tried searching to no avail. I guess my searching skills are fuubar ;))
Merry christmas =)
© Stack Overflow or respective owner