tags:

views:

116

answers:

1

SQL query:

select ApplicationNumber,pri_indicator,count(*) from customer
group by ApplicationNumber,pri_indicator

How do I do this in LINQ?

I see plenty of results on using a simple group by to count a single field, but can't seem to find any or figure out how to do multiple fields.

+9  A: 

Just:

var query = from customer in db.Customers
            group customer by 
                 new { customer.ApplicationNumber, customer.PriIndicator }
                 into grouped
            select new { grouped.Key.ApplicationNumber,
                         grouped.Key.PriIndicator,
                         Count = grouped.Count() };

should work, I think.

Jon Skeet
+1 Too quick :)
Andrew Hare
very nice, how would you attack a having count(*)>1?
Maslow
After the "into grouped" line, add where grouped.Count() > 1
AndyMcKenna