Linq分组和投影查询

时间:2011-10-22 13:57:32

标签: linq linq-to-entities

我有一个EF“通话”课程,其中包含日期,电话号码,来电者姓名和通话费用。

我的目标是通过电话号码对呼叫进行分组,并将总费用相加,并获取40个最昂贵的呼叫的结果。我已经完成了所有这些工作,但是我在投影方面遇到了问题,如何才能访问来电者姓名?

var query =
(

from call in model.Calls
group call by call.TelephoneNumber into g
orderby g.Sum(gr => gr.ActualCost) descending

select new

{
TelephoneNumber = g.Key,
CallerName = ???
Cost = (g.Sum(gr => gr.ActualCost)),
TotalNumbers = g.Count(),
}

).Take(40);

1 个答案:

答案 0 :(得分:0)

您还需要CallerName上的组:

var query =
  (from call in model.Calls
  group call by new { call.TelephoneNumber, call.CallerName }  into g
  orderby g.Sum(gr => gr.ActualCost) descending
  select new
  {
    TelephoneNumber = g.Key.TelephoneNumber,
    CallerName = g.Key.CallerName,
    Cost = (g.Sum(gr => gr.ActualCost)),
    TotalNumbers = g.Count(),
  }).Take(40);