LINQ GROUPBY与几个层次对象 [英] LINQ GroupBy on object with several levels

查看:147
本文介绍了LINQ GROUPBY与几个层次对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得声明如下几类:

public class UserSpend
{
    public string UserName{ get; set; }
    public MonthSpend[] Spend { get; set; }

}

public class MonthSpend
{
    public DateTime Month { get; set; }
    public SpendDetail[] Detail { get; set; }

}

public class SpendDetail
{
    public string Description { get; set; }
    public decimal Amount { get; set; }
}



正如你可以看到有几个层次。我宣布这样一个变量:

which as you can see has several levels. I have declared a variable like this:

UserSpend[] allSpend;



我来填充,这样我有几个UserSpend的,每个有几个MonthSpend的,每个有几个SpendDetail的。因此,数据看起来是这样的:

which I populate so that I have several UserSpend's which each have several MonthSpend's which each have several SpendDetail's. So the data looks something like this:

allSpend =
UserSpend (UserName = "Bob", Spend = [
        MonthSpend (Month = "01/09/12", Detail = [
            SpendDetail (Description = "Local", Amount = "12.00"),
            SpendDetail (Description = "National", Amount = "7.00") ]
        MonthSpend (Month = "01/10/12", Detail = [
            SpendDetail (Description = "Local", Amount = "8.00"),
            SpendDetail (Description = "Mobile", Amount = "3.00"),
            SpendDetail (Description = "National", Amount = "15.00") ]
        MonthSpend (Month = "01/11/12", Detail = [
            SpendDetail (Description = "Local", Amount = "16.00"),
            SpendDetail (Description = "National", Amount = "5.00") ] ]
UserSpend (UserName = "Jack", Spend = [
        MonthSpend (Month = "01/09/12", Detail = [
            SpendDetail (Description = "Local", Amount = "1.00"),
            SpendDetail (Description = "National", Amount = "4.00"),
            SpendDetail (Description = "International", Amount = "17.00") ]
        MonthSpend (Month = "01/10/12", Detail = [
            SpendDetail (Description = "Local", Amount = "2.00"),
            SpendDetail (Description = "Mobile", Amount = "7.00"),
            SpendDetail (Description = "International", Amount = "5.00") ]
        MonthSpend (Month = "01/11/12", Detail = [
            SpendDetail (Description = "Local", Amount = "6.00"),
            SpendDetail (Description = "National", Amount = "2.00") ] ]

我想什么要做的就是填充这个变量:

What I'd like to do is populate this variable:

public class UserDataPoint
{
    public string User { get; set; }
    public string Category { get; set; }
    public int Spend { get; set; }
}

UserDataPoint[] userData;



让用户数据包含了这样的事情:

so that userData contains something like this:

userData = UserDataPoint (
[User = "Bob", Category = "Local", Spend = 36.00]
[User = "Bob", Category = "Mobile", Spend = 3.00]
[User = "Bob", Category = "National", Spend = 27.00]
[User = "Jack", Category = "Local", Spend = 9.00]
[User = "Jack", Category = "Mobile", Spend = 6.00]
[User = "Jack", Category = "National", Spend = 7.00]
[User = "Jack", Category = "International", Spend = 22.00]

因此,对于每个用户名,我想通过描述组和总结的金额。我想学习LINQ,并已尝试使用GROUPBY来实现这一点,但我没有得到任何地方,请会有人提供如何实现这一点的例子。非常感谢,科林·

So for each UserName I'd like to group by Description and sum the Amount. I'm trying to learn LINQ and have been trying to use GroupBy to achieve this, but I'm not getting anywhere. Please could someone provide an example of how to achieve this. Many thanks, Colin.

推荐答案

随着LINQ语法查询:

With syntax query in LINQ:

userData = from spend in allSpend
           from monthSpend in spend.Spend
           from spendDetail in monthSpend.Detail
           group spendDetail by new {spend, spendDetail.Description} into g
           select new UserDataPoint
           {
               User = g.Key.spend.UserName,
               Category = g.Key.Description,
               Spend = g.Sum(t => t.Amount)
           };



这里的困难是由一群多个列(用户名和类)。它使用匿名类型的组...所做{花,spendDetail.Description} 。请参见 #1

下面是一个更好的版本(无需复杂的 GROUPBY

Here is a better version (no need for a complex GroupBy):

userData = from spend in allSpend
           from userDataPoint in
               (from monthSpend in spend.Spend
               from spendDetail in monthSpend.Detail
               group spendDetail by spendDetail.Description into g
               select new UserDataPoint
               {
                   User = spend.UserName,
                   Category = g.Key,
                   Spend = g.Sum(t => t.Amount)
               })
           select userDataPoint;

这篇关于LINQ GROUPBY与几个层次对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆