Translate SQL to lambda LINQ with GroupBy and Average

average, c#, group-by, linq

Solution

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 
         })

Problem

I spend a few hours trying to translate simple SQL to lambda LINQ ``` SELECT ID, AVG(Score) FROM myTable GROUP BY ID ``` Any idea?

Original source