Last post Nov 03, 2008 07:33 PM by JMayo
Nov 03, 2008 11:26 AM|odubhgaill2|LINK
Hi I have a SQL statement which joins a Category and Product table. It returns the CategoryID and the number of products in that category.
COUNT(Product.ProductID) AS NumberProducts
From Category Inner Join Product on Category.CategoryID = Product.CategoryID
GROUP BY Category.CategoryID
I'm trying to write a LINQ query to do the same. What should I Group By and where do I call the Count function in LINQ query?
Nov 03, 2008 07:33 PM|JMayo|LINK
How about this:
var prodCounts =
from cat in ctx.Categories
join prod in ctx.Products
on cat.CategoryID equals prod.CategoryID
group cat by cat.CategoryID into catGp
CategoryID = catGp.Key,
NumberProducts = catGp.Count()