【发布时间】:2017-01-12 14:14:53
【问题描述】:
我有一个以下类型的 SQL 查询,我试图在我的 C# 应用程序中使用 LINQ 获取数据
select
sum(CASE when outcome = 0 then samplingweight else 0 end) tpcount,
sum(CASE when outcome = 1 then samplingweight else 0 end) fpcount,
sum(CASE when outcome = 2 then samplingweight else 0 end) mismatch,
sum(CASE when outcome = 3 then samplingweight else 0 end) fncount,
sum(CASE when outcome = 4 then samplingweight else 0 end) tncount,
sum(samplingweight) totalweight,
__date,
scenario from MyTable
where __date = '30-Dec-16 12:00:00 AM' and scenario = 'parcels'
group by __date, scenario
注意:samplingweight 是 DB 中的双精度值。
我正在尝试并且能够达到以下状态
from pr in this.context.ComputedPrSet
where
pr.Date.Equals(date) && pr.Scenario.Equals(scenario)
group pr by new { pr.Date, pr.Scenario }
into grp
select new
{
grp.Key.Date,
grp.Key.Scenario,
TruePositiveCount = grp.Sum(x => x.SamplingWeight) //I am stuck here. Not sure how to aggregate the properties
};
注意:上述 LINQ 查询中的date 和scenario 被作为参数传递给执行函数。
关于如何进行的任何想法或提示?
【问题讨论】:
-
grp.Where(x => x.outcome == 1).Sum(x => x.SamplingWeight)
标签: c# .net linq group-by aggregate