LINQ Queries with GROUP BY, INNER JOIN, COUNT and SUM: Examples
Examples of different LINQ queries in C#: GROUP BY, INNER JOIN, COUNT and SUM Setup the LINQPad against the Northwind database. 1. LINQ query with GROUP BY, JOIN and SUM. Find the quantities required for each product to fulfill all the orders. Here the tables we have to use are Products and Order Details. Both of these tables can be joined on ProductID key and grouped by Product Name to make sure that the Quantity is calculated against a named product instead of ID. The SQL query for the above requirement is : s elect A.ProductName,sum(B.Quantity) from Products P join "Order Details" OD on A.ProductID=B.ProductID group by A.ProductName The equivalent LINQ query for the above is: from p in Products join od in OrderDetails on p.ProductID equals od.ProductID group od by p.ProductName into g select new { ProductName=g.Key, Quanti=g.Sum(a => a.Quantity)} 2. LIN