Linq to SQL Group by and Sum in Select

前端 未结 1 1026
失恋的感觉
失恋的感觉 2021-02-09 10:52

I need to convert that SQL Query into Linq:

SELECT
    SUM([ArticleAmount]) as amount
    ,[ArticleName]
FROM [DB].[dbo].[OrderedArticle]

group by articlename

         


        
相关标签:
1条回答
  • 2021-02-09 11:19

    You are getting this error because the Grouping doesn't return IEnumerable<OrderedArticle> but IEnumerable<IGrouping<string, OrderedArticle>>

    You need to change your code to use 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();
    
    0 讨论(0)
提交回复
热议问题