开发者

How do I construct a LINQ with multiple GroupBys?

I have an entity that looks like this:

public partial class MemberTank
{
    public int Id { get; set; }
    public int AccountId { get; set; }
    public int Tier { get; set; }
    public string Class { get; set; }
    public string TankName { get; set; }
    public int Battles { get; set; }
    public int Victories { get; set; }
    public System.DateTime LastUpdated { get; set; }
}

A tiny sample of the data:

 Id   A开发者_如何学运维ccountId   Tier   Class  TankName   Battles  Victories
 ---  ---------   ----   -----  ---------  -------  ----------
 1    432423      5      Heavy  KV         105      58
 2    432423      6      Heavy  IS         70       39
 3    544327      5      Heavy  KV         200      102
 4    325432      7      Medium KV-13      154      110
 5    432423      7      Medium KV-13      191      101

Ultimately I am trying to get a result that is a list of tiers, within the tiers is a list of classes, and within the class is a distinct grouping of the TankName with the sums of Battles and Victories.

Is it possible to do all this in a single LINQ statement? Or is there another way to easily get the result? (I know I can easily loop through the DbSet several times to produce the list I want; I am hoping for a more efficient way of getting the same result with LINQ.)


This should do it:

var output = from mt in MemberTanks
             group by {mt.Tier, mt.Class, mt.TankName} into g
             select new { g.Key.Tier, 
                          g.Key.Class, 
                          g.Key.TankName, 
                          Fights = g.Sum(mt => mt.Battles), 
                          Wins = g.Sum(mt=> mt.Victories
                        };


You could also use Method syntax. This should give you the same as @TheEvilGreebo

var result = memberTanks.GroupBy(x => new {x.Tier, x.Class, x.TankName})
                        .Select(g => new { g.Key.Tier, 
                                           g.Key.Class, 
                                           g.Key.TankName, 
                                           Fights = g.Sum(mt => mt.Battles), 
                                           Wins = g.Sum(mt=> mt.Victories)
                        });

Which syntax you use comes down to preference. Remove the .Select to return the IGrouping which will enable you to enumerate the groups

var result = memberTanks.GroupBy(x => new {x.Tier, x.Class, x.TankName})


I kept trying to get useful results our of the The Evil Greebo's answer. While the answer does yield results (after fixing the compilation issues mentioned in responses) it doesn't give me what I was really looking for (meaning I didn't explain myself well enough in the question).

Feanz left a comment in my question to check out the MS site with LINQ examples and, even though I thought I had looked there before, this time I found their example of nested group bys and I tried it their way. The following code gives me exactly what I was looking for:

var result = from mt in db.MemberTanks
             group mt by mt.Tier into tg
             select new
             {
                 Tier = tg.Key,
                 Classes = from mt in tg
                           group mt by mt.Class into cg
                           select new
                           {
                               Class = cg.Key,
                               TankTypes = from mt in cg
                                           group mt by mt.TankName into tng
                                           select new
                                           {
                                               TankName = tng.Key,
                                               Battles = tng.Sum(mt => mt.Battles),
                                               Victories = tng.Sum(mt => mt.Victories),
                                               Count = tng.Count()
                                           }
                           }
             };

I'll leave the answer by Mr. Greebo checked as most people will likely get the best results from that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜