From the course: .NET Essentials: LINQ for Databases

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Organize query results into groups with GroupBy

Organize query results into groups with GroupBy - .NET Tutorial

From the course: .NET Essentials: LINQ for Databases

Start my 1-month free trial

Organize query results into groups with GroupBy

- [Instructor] We can use the group by extension method or the group by clause in a query expression to split our query into groups based on a key that we provide. Now, one of the rules of query expressions is that you always need to end your query expression with a select or a group by. So what I'm doing is I'm doing my grouping on one line in the expression, and then once I have the group, then I'm selecting out the group. There are other alternatives syntax, but that's the one I'm using for this demo. My goal is to split into groups based on the first letter of the product names. Let's start by taking a look at what we get when we just run this basic query. This will select out the product name and the unit price. My results are, I get 77 items. Now the next thing I want to do is somehow make a group based on the first letter. So the way we do that is we use the group, keyword. So I say group P by this criteria, this…

Contents