具有多个级别的对象上的LINQ GroupBy
本文关键字:对象 LINQ GroupBy | 更新日期: 2023-09-27 18:24:54
我有一些类声明如下:
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,每个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)
};
这里的困难在于按多个列(用户名和类别)进行分组。这是在group ... by {spend, spendDetail.Description}
中使用匿名类型完成的。参见堆叠溢出
更新
这里有一个更好的版本(不需要复杂的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;