GroupBy 带条件分组求和

时间:2021-11-05 15:10:53
 public List<Tuple<string, Tuple<decimal, decimal, decimal, decimal>, Tuple<decimal, decimal, decimal, decimal>>> GetTupeDate(List<ASSENTS_PUBLIC> publicList) 
{
//按条件分组求和
var grouplist = publicList.GroupBy(t => t.WORKUNIT).Select(g => new
{
unitName
= unitDal.FindModelByID(Convert.ToInt32(g.FirstOrDefault().WORKUNIT)).UNITNAME,

Sum
= g.Where(c => c.USESTATE == "闲置").Count(),

originalvalue
= Convert.ToDecimal(g.Where(c => c.USESTATE == "闲置").Sum(t => t.ORIGINALVALUE)),

networth
= Convert.ToDecimal(g.Where(c => c.USESTATE == "闲置").Sum(t => t.NETWORTH)),

impairment
= Convert.ToDecimal(g.Where(c => c.USESTATE == "闲置").Sum(t => t.IMPAIRMENT)),


Sum1
= g.Where(c => c.USESTATE == "出租" || c.USESTATE == "部分出租").Count(),

originalvalue1
= Convert.ToDecimal(g.Where(c => c.USESTATE == "出租" || c.USESTATE == "部分出租").Sum(t => t.ORIGINALVALUE)),

networth1
= Convert.ToDecimal(g.Where(c => c.USESTATE == "出租" || c.USESTATE == "部分出租").Sum(t => t.NETWORTH)),

impairment1
= Convert.ToDecimal(g.Where(c => c.USESTATE == "出租" || c.USESTATE == "部分出租").Sum(t => t.IMPAIRMENT))
});
//List<Tuple<单位名称, Tuple<项数,闲置原值, 闲置净值, 闲置减值准备>, Tuple<项数,出租或部分出租原值, 出租或部分出租净值, 出租或部分出租减值准备>>>
List<Tuple<string, Tuple<decimal, decimal, decimal, decimal>, Tuple<decimal, decimal, decimal, decimal>>> TupList = new List<Tuple<string, Tuple<decimal, decimal, decimal, decimal>, Tuple<decimal, decimal, decimal, decimal>>>();
foreach (var group in grouplist)
{
Tuple
<decimal, decimal, decimal, decimal> temp = new Tuple<decimal, decimal, decimal, decimal>(group.Sum, group.originalvalue, group.networth, group.impairment);
Tuple
<decimal, decimal, decimal, decimal> temp1 = new Tuple<decimal, decimal, decimal, decimal>(group.Sum1, group.originalvalue1, group.networth1, group.impairment1);
Tuple
<string, Tuple<decimal, decimal, decimal, decimal>, Tuple<decimal, decimal, decimal, decimal>> temp2 = new Tuple<string, Tuple<decimal, decimal, decimal, decimal>, Tuple<decimal, decimal, decimal, decimal>>(group.unitName, temp, temp1);
TupList.Add(temp2);
}
return TupList;
}