Translate SQL to lambda LINQ with GroupBy and Average

前端 未结 3 1964
心在旅途
心在旅途 2021-02-13 13:14

I spend a few hours trying to translate simple SQL to lambda LINQ

SELECT ID, AVG(Score) FROM myTable
GROUP BY ID

Any idea?

3条回答
  •  长情又很酷
    2021-02-13 13:38

    var _result =   from a in myTable
                    group a by a.ID into g
                    select new
                    {
                        ID = g.Key.ID,
                        AverageResult = g.Average(x => x.Score)
                    }
    

提交回复
热议问题