LINQ到实体模型的分组

时间:2011-10-05 08:46:00

标签: linq linq-to-entities

我在实体模型对象中有以下字段

MONTH_CHAR char(1) AVG_BALANCE int PROD_CAT_ID int FLG_PERS_COMM聊天(1) ACCOUNT_COUNT int

我希望通过MONTH_CHAR列分组,我将为此

编写以下SQL查询

SELECT MONTH_CHAR, SUM(AVG_BALANCE) AS AVG_BALANCE, MAX(PROD_CAT_ID) AS PROD_CAT_ID, MAX(ACCOUNT_COUNT) AS ACCOUNT_COUNT, FROM contactSummary WHERE PROD_CAT_ID = 1 GROUP BY MONTH_CHAR

我希望将此查询转换为LINQ查询。

提前致谢

1 个答案:

答案 0 :(得分:2)

from contact in context.Contacts
group contact by contact.Month_Char into g
select new 
{
    MonthChar = g.Key,
    AvgBalance = g.Average(x=>x.Avg_Balance)
    CatID = g.Max(x=>x.Prod_Cat_ID)
    AccountCount = g.Max(x=>x.Account_Count)
}