Thursday, January 8, 2009

LINQ To SQL : Group By.

 

The SQL GROUP BY clause can be used in a SELECT statement to collect data across multiple TABLES and group the results by one or more columns.  here is how we can achieve this in LINQ TO SQL  in simple form.

Here we use Customer and Order  tables from NorthWind (Free DB We can play with).

 

image

Create the DataContext  and write the query to select records from Customer  and Order Tables. assign this result to customerRecords.  now call the Extension method GropBy on customerRecords   here we are gouping the result by customer Id. and then loop through the result .

 

image

 

Here is the output of the above program.

image

 

Thanks

SreenivasaRagavn.

No comments: