日期:2014-05-20 浏览次数:20763 次
var staffs = from p in db.Staff group p by new {Name=p.Name,Departments=p.Departments} into g select new { Name=g.Key.Name, Departments=g.Key.Departments, TotalWage=g.Sum(p=>p.Wage), TotalAward=g.Sum(p=>p.Award) };
------解决方案--------------------
var array = new Foo[]{
new Foo(){ Name="张三",Deprt="业务部", Salary=2200, Bonus=300},
new Foo(){ Name="李四",Deprt="资源部", Salary=3100, Bonus=200},
new Foo(){ Name="王五",Deprt="工 会", Salary=2800, Bonus=100},
new Foo(){ Name="赵六",Deprt="财务部", Salary=3300, Bonus=500},
new Foo(){ Name="张三",Deprt="业务部", Salary=1000, Bonus=100},
new Foo(){ Name="马七",Deprt="工程部", Salary=3300, Bonus=300},
new Foo(){ Name="王五",Deprt="工 会", Salary=1200, Bonus=300}
};
var table = from item in array
group new { Salary = item.Salary, Bonus = item.Bonus }
by new { Name = item.Name, Deprt = item.Deprt } into grouping
select new
{
Name = grouping.Key.Name,
Depart = grouping.Key.Deprt,
Salary = grouping.Sum(x=>x.Salary),
Bonus = grouping.Sum(x=>x.Bonus)
};
var table = array .GroupBy(item => new { Name = item.Name, Deprt = item.Deprt }) .Select(grouping => new { Name = grouping.Key.Name, Depart = grouping.Key.Deprt, Salary = grouping.Sum(x => x.Salary), Bonus = grouping.Sum(x => x.Bonus) });