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 - .NET Tutorial
From the course: .NET Essentials: LINQ for Databases
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
-
-
-
-
-
-
-
(Locked)
Projection, get fewer columns2m 19s
-
(Locked)
Paging with Take and Skip1m 49s
-
(Locked)
Use Find for faster search by key1m 58s
-
(Locked)
Use EF navigation properties instead of joins3m 4s
-
(Locked)
Use the Contains method for1m 36s
-
(Locked)
Organize query results into groups with GroupBy3m 7s
-
(Locked)
Flatten child collections with SelectMany1m 36s
-
(Locked)
Find data within a date range1m 20s
-
(Locked)
-