如何在具有多个表的 Linq 查询中使用聚合运算符?
How can I use Aggregate Operators in Linq queries with multiple tables?
我想在下面的代码中实现 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
然后我在 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) })
但是我遇到了一些问题,所以如果您有任何建议,请随时与我分享,
请阅读我对问题的评论。我认为您应该这样更正您的查询:
select new {Name = g.Key, SISScore = g.Average(x=>x.TotalPoint) })
因为 SUM(Field)/Count(Field)
意味着你想获得 平均值 ;)
我想在下面的代码中实现 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
然后我在 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) })
但是我遇到了一些问题,所以如果您有任何建议,请随时与我分享,
请阅读我对问题的评论。我认为您应该这样更正您的查询:
select new {Name = g.Key, SISScore = g.Average(x=>x.TotalPoint) })
因为 SUM(Field)/Count(Field)
意味着你想获得 平均值 ;)