Entity Framework 平均值,按查询分组
Entity Framework average, group by query
我正在尝试将 SQL 查询转换为 ASP.NET MVC 5 控制器的 Linq。
SELECT TOP (10)
E.Surname, E.Name, E.Patronymic, AVG(CAST(IEEResult1 AS FLOAT))
FROM
IEEResults AS I
JOIN
Enrollee AS E ON E.Id = I.EnrolleeId
GROUP BY
I.EnrolleeId, E.Surname, E.Name, E.Patronymic
ORDER BY
AVG(CAST(IEEResult1 AS FLOAT)) DESC;
这就是答案。非常感谢afrazier!
var result = db.IEEResults
.Join(db.Enrollees, r => r.EnrolleeId, e => e.Id, (r, e) => new { e.Id, e.Surname, e.Name, e.Patronymic, r.IEEResult1 })
.GroupBy(a => new { a.Id, a.Surname, a.Name, a.Patronymic })
.Select(gr => new IEEResultsDTO
{
Id = gr.Key.Id,
Surname = gr.Key.Surname,
Name = gr.Key.Name,
Patronymic = gr.Key.Patronymic,
AvgResult = gr.Average(a => a.IEEResult1)
})
.OrderByDescending(b => b.AvgResult)
.Take(10)
.ToList().AsEnumerable();
return View(result);
不要将 IEEResult1
放入您的组中,因为您要对这些项目的集合进行操作。我不太擅长查询语法,但这应该是 lambda 语法的有效查询。
var result = db.IEEResults
.Join(db.Enrollees, r => r.EnrolleeId, e => e.Id, (r, e) => new { e.Id, e.Surname, e.Name, e.Patronymic, r.IEEResult1 })
.GroupBy(a => new { a.Id, a.Surname, a.Name, a.Patronymic })
.Select(gr => new {
gr.Key.Id, gr.Key.Surname, gr.Key.Name, gr.Key.Patronymic,
AvgResult => gr.Average(a => a.IEEResult1)
})
.OrderByDescending(b => b.AvgResult)
.Take(10)
.ToList();
我正在尝试将 SQL 查询转换为 ASP.NET MVC 5 控制器的 Linq。
SELECT TOP (10)
E.Surname, E.Name, E.Patronymic, AVG(CAST(IEEResult1 AS FLOAT))
FROM
IEEResults AS I
JOIN
Enrollee AS E ON E.Id = I.EnrolleeId
GROUP BY
I.EnrolleeId, E.Surname, E.Name, E.Patronymic
ORDER BY
AVG(CAST(IEEResult1 AS FLOAT)) DESC;
这就是答案。非常感谢afrazier!
var result = db.IEEResults
.Join(db.Enrollees, r => r.EnrolleeId, e => e.Id, (r, e) => new { e.Id, e.Surname, e.Name, e.Patronymic, r.IEEResult1 })
.GroupBy(a => new { a.Id, a.Surname, a.Name, a.Patronymic })
.Select(gr => new IEEResultsDTO
{
Id = gr.Key.Id,
Surname = gr.Key.Surname,
Name = gr.Key.Name,
Patronymic = gr.Key.Patronymic,
AvgResult = gr.Average(a => a.IEEResult1)
})
.OrderByDescending(b => b.AvgResult)
.Take(10)
.ToList().AsEnumerable();
return View(result);
不要将 IEEResult1
放入您的组中,因为您要对这些项目的集合进行操作。我不太擅长查询语法,但这应该是 lambda 语法的有效查询。
var result = db.IEEResults
.Join(db.Enrollees, r => r.EnrolleeId, e => e.Id, (r, e) => new { e.Id, e.Surname, e.Name, e.Patronymic, r.IEEResult1 })
.GroupBy(a => new { a.Id, a.Surname, a.Name, a.Patronymic })
.Select(gr => new {
gr.Key.Id, gr.Key.Surname, gr.Key.Name, gr.Key.Patronymic,
AvgResult => gr.Average(a => a.IEEResult1)
})
.OrderByDescending(b => b.AvgResult)
.Take(10)
.ToList();