我的公式如下:
Conversion = Settled / (Settled + Cancelled)
如何在LINQ语句中执行此操作?
这是我的代码:
WIP = g.Where(v => v.StatusCategoryID == 40).Sum(c => c.Amount) ?? 0,
Settled = g.Where(v => v.StatusCategoryID == 11).Sum(c => c.Amount) ?? 0,
Cancelled = g.Where(v => v.StatusCategoryID == 10).Sum(c => c.Amount) ?? 0,
答案 0 :(得分:0)
您可以使用Aggregate
尝试这样的事情(虽然感觉有点滥用):
var agg = g.Aggregate(new { wip = 0, settled = 0, cancelled = 0 }, (a, curr) =>
{
return new
{
wip = a.wip + a.StatusCategoryID == 40 ? curr.Amount : 0,
settled = a.settled + a.StatusCategoryID == 11? curr.Amount : 0,
cancelled = a.cancelled + a.StatusCategoryID == 10? curr.Amount : 0,
};
});
然后你可以:
var Conversion = agg.settled / (agg.settled + agg.cancelled);
和
var WIP = agg.wip;
这里的优势在于您只需在一个循环中对所有三个类别进行求和。我不确定我发现比使用foreach
循环要好得多。