C# LINQ中的分组聚合函数

C# LINQ中的分组聚合函数,c#,.net,linq,group-by,aggregate,C#,.net,Linq,Group By,Aggregate,我有一个以下类型的SQL查询,我试图使用LINQ在我的C#应用程序中获取数据 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,

我有一个以下类型的SQL查询,我试图使用LINQ在我的C#应用程序中获取数据

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
作为执行函数的参数


关于如何进行的任何想法或提示?

您可以使用
?:
操作符在执行
零件时执行
案例

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.Outcome == 0?x.SamplingWeight:0)
};

您可以使用
?:
运算符在执行
零件时执行
案例

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.Outcome == 0?x.SamplingWeight:0)
};

您有两个容易看到的选项:

  • 求和前使用
    .Where()
    子句筛选grp
  • .Sum()

我认为前者更可读,后者更匹配SQL。根据您的提供者的不同,它们可能会转换为不同的SQL。如果性能有问题,请确保评估结果SQL

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.Where(x => x.outcome == 0).Sum(x => x.SamplingWeight),
               FPCount = grp.Sum(x => x.outcome == 1 ? x.SamplingWeight : 0),
           };
第三个选项是在DB中按日期、场景和结果分组,然后重新将
Select()
放入代码中所需的对象中,如下所示:

this.Context.ComputedPrSet
    .Where(pr => pr.Date == date && pr.Scenario == scenario)
    .GroupBy(pr => new { pr.Date, pr.Scenario, pr.Outcome })
    .Select(grp => new {
        grp.Key.Date,
        grp.Key.Scenario,
        grp.Key.Outcome,
        Count = grp.Sum(pr => pr.SamplingWeight)
    })
    .AsEnumerable()
    // .AsEnumerable() will "close" the query in LINQ.
    // Further operations are done via LINQ to Objects after fetching the prior results from your DB
    .GroupBy(e => new { e.Date, e.Scenario })
    .Select(g => new {
        g.Key.Date,
        g.Key.Scenario,
        TruePositiveCount = g.FirstOrDefault(e => e.Outcome == 0),
        FPCount = g.FirstOrDefault(e => e.Outcome == 1),
    });

您有两个容易看到的选项:

  • 求和前使用
    .Where()
    子句筛选grp
  • .Sum()

我认为前者更可读,后者更匹配SQL。根据您的提供者的不同,它们可能会转换为不同的SQL。如果性能有问题,请确保评估结果SQL

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.Where(x => x.outcome == 0).Sum(x => x.SamplingWeight),
               FPCount = grp.Sum(x => x.outcome == 1 ? x.SamplingWeight : 0),
           };
第三个选项是在DB中按日期、场景和结果分组,然后重新将
Select()
放入代码中所需的对象中,如下所示:

this.Context.ComputedPrSet
    .Where(pr => pr.Date == date && pr.Scenario == scenario)
    .GroupBy(pr => new { pr.Date, pr.Scenario, pr.Outcome })
    .Select(grp => new {
        grp.Key.Date,
        grp.Key.Scenario,
        grp.Key.Outcome,
        Count = grp.Sum(pr => pr.SamplingWeight)
    })
    .AsEnumerable()
    // .AsEnumerable() will "close" the query in LINQ.
    // Further operations are done via LINQ to Objects after fetching the prior results from your DB
    .GroupBy(e => new { e.Date, e.Scenario })
    .Select(g => new {
        g.Key.Date,
        g.Key.Scenario,
        TruePositiveCount = g.FirstOrDefault(e => e.Outcome == 0),
        FPCount = g.FirstOrDefault(e => e.Outcome == 1),
    });
grp.Where(x=>x.output==1).Sum(x=>x.SamplingWeight)
grp.Where(x=>x.output==1).Sum(x=>x.SamplingWeight)