LINQ查询到Group和SUM值

时间:2014-12-31 22:56:43

标签: c# linq

我有这样的SQL查询,我正在尝试使用LINQ将其转换为C#代码,但我不知道如何获得SUM&使用LINQ的平均值。

SQL查询

Select SID, SName, EID, SUM(CAST(EPoints AS Decimal(18, 2)))/COUNT(EFID), EFID,  
SUM(CASE WHEN FValue = 'Y' THEN 1 ELSE 0 END) 'Y',  
SUM(CASE WHEN FValue = 'N' THEN 1 ELSE 0 END) 'N'
from TestTable where ISNULL(FValue, '') != ''
Group By SID, SName, EID, EFID
Order By SID, SName, EID, EFID

此外,我已将上述SQL查询部分转换为Linq表达式,但卡在SUM&平均部分是这样,有人可以告诉我该怎么做吗?

var sortedTable = testTableData.AsEnumerable()
.Where(r => r.Field<string>("FValue") != "")
.GroupBy(r => new
 {
     SID = r.Field<int>("SID"),
     SName = r.Field<string>("SName"),
     EID = r.Field<int>("EID"),
     EFID = r.Field<int>("EFID")   
 })
.OrderBy(g => g.Key.SID)
.ThenBy(g => g.Key.SName)
.ThenBy(g => g.Key.EID)
.ThenBy(g => g.Key.EFID)                                        
.Select(g => new {
    SID = g.Key.SID,
    SName = g.Key.SName,
    EID = g.Key.EID,
    EFID = g.Key.EFID                                            
});

3 个答案:

答案 0 :(得分:1)

.select部分中,您可以使用内置的linq聚合函数Average和Sum来进行所需的聚合,我不确定以下是否编译,但它应该在逻辑上满足您的需求:

   .Select(g => new {
                SID = g.Key.SID,
                SName = g.Key.SName,
                EID = g.Key.EID,
                EFID = g.Key.EFID,
                EpointsAverage = g.Average(x => x.EPoints),
                YSum = g.Sum(x => (x.FValue == 'Y' ? 1 : 0)),
                NSum = g.Sum(x => (x.FValue == 'N' ? 1 : 0))
            });

让我知道它是怎么回事,如果有任何问题需要相应地更新我的答案

答案 1 :(得分:0)

在这里,您需要使用Entity框架中提供的聚合函数将结果作为::

var sortedTable = testTableData.AsEnumerable()
                    .Where(r => r.EFValue!=null && r.EFValue!= "")
                    .GroupBy(r => new
                     {
                         SID = r.SID,
                         SName = r.SName,
                         EID = r.EID,
                         EFID = r.EFID   
                     })
                    .OrderBy(g => g.Key.SID)
                    .ThenBy(g => g.Key.SName)
                    .ThenBy(g => g.Key.EID)
                    .ThenBy(g => g.Key.EFID)                                        

                    .Select(g => new {
                        SID = g.Key.SID,
                        SName = g.Key.SName,
                        EID = g.Key.EID,
                        EFID = g.Key.EFID,

                        Avg=g.Average(x=>x.EPoints),
                        Y=g.Where(x=>x.FValue=="Y").Sum(x=>x.FValue),
                        N=g.Where(x=>x.FValue=="N").Sum(x=>x.FValue)
                    });

答案 2 :(得分:0)

它现在正在工作......请参阅我使用的以下代码:

var sortedTable = testTableData.AsEnumerable()
.Where(r => r.Field<string>("FValue") != "")
.GroupBy(r => new
 {
     SID = r.Field<int>("SID"),
     SName = r.Field<string>("SName"),
     EID = r.Field<int>("EID"),
     EFID = r.Field<int>("EFID")   
 })
.OrderBy(g => g.Key.SID)
.ThenBy(g => g.Key.SName)
.ThenBy(g => g.Key.EID)
.ThenBy(g => g.Key.EFID)
.Select(g => new {
                        SID = g.Key.SID,
                        SName = g.Key.SName,
                        EID = g.Key.EID,
                        EFID = g.Key.EFID,    
                        Avg = g.Average(x => decimal.Parse(x.Field<string>("EPoints"))),
                        YesCount = g.Sum(x => x.Field<string>("FValue") == "Y" ? 1 : 0),
                        NoCount = g.Sum(x => x.Field<string>("FValue") == "N" ? 1 : 0)
                    });