我有一些像这样声明的类:
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]
因此,对于每个UserName,我想按描述进行分组并对金额求和。我正在尝试学习LINQ并且一直在尝试使用GroupBy来实现这一点,但我没有得到任何结果。请有人提供一个如何实现这一目标的例子。非常感谢,科林。
答案 0 :(得分:3)
在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}
中的匿名类型完成的。见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;