ASPNET MVC与EF-SUM&通过...分组

时间:2017-01-07 15:49:50

标签: sql asp.net-mvc entity-framework

我尝试将该SQL查询转换为链接,但它不起作用。

SELECT
SUM([Quantity]) as qt
,[ArticleID]
FROM [DB].[dbo].[Location]
Group by ArticleID
Order by qt

List <Location> articles = contexteEF.Location.GroupBy(l => l.ArticleID).Select(a => new { qt = a.Sum(b => b.Quantity), ArticleID = a.Key }).OrderByDescending(a => a.qt).ToList();
你能帮帮我吗?请!

由于

1 个答案:

答案 0 :(得分:0)

您正在尝试将动态对象列表转换为List<Location>

我认为该位置包含qtArticleId属性?在这种情况下,不要创建动态对象,创建位置。代码未经过测试,但有类似的内容:

List <Location> articles = contexteEF.Location.GroupBy(l => l.ArticleID).Select(a => new Location() { qt = a.Sum(b => b.Quantity), ArticleID = a.Key }).OrderByDescending(a => a.qt).ToList();