其他分享
首页 > 其他分享> > LINQ GroupBy在具有多个级别的对象上

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; }
}

如您所见,它具有多个级别.我已经声明了这样的变量:

UserSpend[] allSpend;

我填充了它,所以我有几个UserSpend,每个都有若干MonthSpend,每个都有多个SpendDetail.因此数据看起来像这样:

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") ] ]

我想做的是填充此变量:

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

UserDataPoint[] userData;

因此userData包含以下内容:

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来实现这一点,但是我却一无所获.请有人提供如何实现此目的的示例.非常感谢,科林.

解决方法:

在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)
           };

这里的困难是按多个列(用户名和类别)分组.该操作是在组中使用匿名类型完成的,该方法由{spend,SpendDetail.Description}组成.查看Stackoverflow

更新资料

这是一个更好的版本(不需要复杂的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,c,group-by
来源: https://codeday.me/bug/20191123/2064151.html