我需要将SQL查询转换为Linq:
SELECT
SUM([ArticleAmount]) as amount
,[ArticleName]
FROM [DB].[dbo].[OrderedArticle]
group by articlename
order by amount desc
我尝试了下面的代码,但我在"a.ArticleName"得到一个错误,说"ArticleName"的定义将丢失。
var sells = orderedArt
.GroupBy(a => a.ArticleName)
.Select(a => new {Amount = a.Sum(b => b.ArticleAmount),Name=a.ArticleName})
.OrderByDescending(a=>a.Amount)
.ToList();
有人和你的想法如何解决这个问题?
谢谢你的帮助!
你得到这个错误,因为分组不返回IEnumerable<OrderedArticle>
,但IEnumerable<IGrouping<string, OrderedArticle>>
您需要更改代码以使用a.Key
:
var sells = orderedArt
.GroupBy(a => a.ArticleName)
.Select(a => new { Amount = a.Sum(b => b.ArticleAmount), Name = a.Key})
.OrderByDescending(a => a.Amount)
.ToList();