English 中文(简体)
Grouping a list of of entities which has a sublist in c#
原标题:

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 want 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 want 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 in case you need to take that into account.

问题回答

Try:

 dataset.SelectMany(p => new {stat=p, daystats=p.DayStats)
        .GroupBy(c => c.stat.Name.SubString(0, c.stat.Name.Length - 5))

A little late, but maybe still useful. RemoveCountry should be an extension method that returns the name of the Stats without the country suffix.

IEnumerable<Stats> aggregated =
    from stat in stats
    group stat by stat.Name.RemoveCountry() into grp
    select new Stats
    {
        Name = grp.Key,
        TotalResources = (from stat in grp select stat.TotalResources).Sum(),
        DayStats = (from stat in grp
                    from dayStat in stat
                    group dayStat by dayStat.Date into dgrp
                    select new DayStats
                    {
                        Date = dgrp.Key,
                        TotalResources = (from dayStat in dgrp select dayStat.TotalResources).Sum()
                    }).ToList()
    };




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签