Kash
Kash

Reputation: 245

Grouping in LINQ to Entity Model

I have following fields in entity model object

MONTH_CHAR char(1) AVG_BALANCE int PROD_CAT_ID int FLG_PERS_COMM chat(1) ACCOUNT_COUNT int

I want group by MONTH_CHAR column, I 'll write following SQL Query for this

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

I want this query to be converted to LINQ Query.

Thanks in advance

Upvotes: 0

Views: 77

Answers (1)

Muhammad Hasan Khan
Muhammad Hasan Khan

Reputation: 35126

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

Upvotes: 2

Related Questions