我有一个看起来像这样的列表。
-100 Smith, Jane $1000
-100 Smith, John $1100.00
-100 Smith, Cole $840.00
-110 Jones, Harry $1270.00
-110 Jones, Diane $870.00
-111 Jones, Richard $1560.00
使用 LINQ,我希望编写一个查询,允许我按家庭对值求和。第一个标识符是家庭 ID。对于上述记录集,我的输出将如下所示:
-100 Smith $2940.00
-110 Jones $2140.00
-111 Jones $1560.00
这是我的代码:
static void Main(string[] args)
{
_fi = FillIncome();
var query = (from f in _fi group f by f.familyId into delta select new {familyId = delta.Key, amount = delta.Sum(x => x.income) });
foreach (var q in query)
{
Console.WriteLine(q);
}}
static List<FamilyIncome> FillIncome()
{
var income = new List<FamilyIncome>();
var fi = new FamilyIncome {familyId = -100, lastname = "Smith", firstname = "Jane", income = 1000};
income.Add(fi);
fi = new FamilyIncome { familyId = -100, lastname = "Smith", firstname = "John", income = 1100 };
income.Add(fi);
fi = new FamilyIncome { familyId = -100, lastname = "Smith", firstname = "Cole", income = 840 };
income.Add(fi);
fi = new FamilyIncome { familyId = -110, lastname = "Jones", firstname = "Harry", income = 1270 };
income.Add(fi);
fi = new FamilyIncome { familyId = -110, lastname = "Jones", firstname = "Diane", income = 970 };
income.Add(fi);
fi = new FamilyIncome { familyId = -111, lastname = "Jones", firstname = "Richard", income = 1600 };
income.Add(fi);
return income;
}