我有一份清单如下
var left = (from T in brndT
join S in brndS
on T.ID equals S.ID
into temp
from S in temp.DefaultIfEmpty()
select new BrandSalesTarget
{
ID = T.ID,
Target = T.Target == null ? 0 : (int)T.Target,
Sales = S != null ? (int)S.Qty : 0
}).ToList();
listOfList.Add(left);
现在我想要ID, TotalTarget, TotalSales
这意味着按列表列出ID, Sum(target), Sum(sales)
。怎么做?
答案 0 :(得分:1)
听起来你想要这样的东西:
var result =
from list in listOfList
from T in list
group T by T.ID into g
select new // possibly select new BrandSalesTarget
{
ID = g.Key,
Target = g.Sum(t => t.Target),
Sales = g.Sum(t => t.Sales)
};
然而,似乎更好的解决方案可能是重构您的代码,以便您可以直接派生此结果,而不是处理List<List<BrandSalesTarget>>
,但没有看到完整的上下文,很难说这看起来如何