2011-10-22 116 views
0

我有一個EF「呼叫」類,它有一個日期,電話號碼,來電者姓名和通話費用。Linq分組和投影查詢

我的目標是按電話號碼對呼叫進行分組,並總計總成本,並將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); 

回答

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); 
+0

非常感謝你的幫助!很簡單! – Trevor