Translate SQL to lambda LINQ with GroupBy and Average

前端 未结 3 1967
心在旅途
心在旅途 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:35

    from t in myTable
    group t by new {
      t.ID
    } into g
    select new {
      Average = g.Average(p => p.Score),
      g.Key.ID
    }
    

    or Lambda

    myTable.GroupBy(t => new  {ID = t.ID})
       .Select (g => new {
                Average = g.Average (p => p.Score), 
                ID = g.Key.ID 
             })
    

提交回复
热议问题