简体   繁体   English

如何在具有多个表的Linq查询中使用聚合运算符?

[英]How can I use Aggregate Operators in Linq queries with multiple tables?

I would like to realize below the codes in T-SQL to Linq queries, 我想在T-SQL到Linq查询的代码下面实现,

select rg.Name, Sum(si.TotalPoint) / Count(si.TotalPoint) from ScoreItem si    
inner join Score s on s.Id = si.ScoreId
inner join ProjectResearchGroup prg on prg.Id = si.ProjectResearchGroupId
inner join RgClone rg on rg.Id = prg.RgCloneId
where rg.Name in (N'26 Gros Kurşun Kalem Stant Skor Kartı'
--,N'A5/A6 Ayaklı Defter Stant Skor Kartı'
--,N'A5/A6 Defter Stant Skor Kartı'
--,N'Adel Metal Stant Skor Kartı')
group by rg.Name
order by Name

Then I have re-wrote them in Linq because I have to use it with EF, 然后我在Linq中重新编写了它们,因为我必须将其与EF一起使用,

(from si in ScoreItem
       join s in Score on si.ScoreId equals s.Id
       join prg in ProjectResearchGroup on si.ProjectResearchGroupId equals prg.Id
       join rg in RgClone on prg.RgCloneId equals rg.Id 
       group rg by rg.Name into g 
   select new {Name = g.Key, SISScore = g.Sum(rg.TotalPoint) / g.Count(rg.TotalPoint) })

But I have some problems, so if you have any advice, please feel to free and share with me, 但是我有一些问题,因此,如果您有任何建议,请放心与我分享,

Please, read my comments to the question. 请阅读我对问题的评论。 I think you should correct your query this way: 我认为您应该通过以下方式更正您的查询:

select new {Name = g.Key, SISScore = g.Average(x=>x.TotalPoint) })

because SUM(Field)/Count(Field) means you want to get average ;) 因为SUM(Field)/Count(Field)表示您想获得平均值 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM