From the course: .NET Essentials: LINQ for Databases
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Flatten child collections with SelectMany - .NET Tutorial
From the course: .NET Essentials: LINQ for Databases
Flatten child collections with SelectMany
- [Narrator] We have seen that the navigation properties in Entity Framework, are useful for getting related information for an entity model. And here's an example, where I dumped out the regions information, and I can see there is the region ID column, and the region description column. And then through the navigation property, I can read more details about how many territories are part of the Eastern region. So there are 19 territories that are in that region, and in the Western region, I've got 15 territories. So that's useful, but you notice how they're in basically a sub collection, they're in a hash set, and that can sometimes make it harder to query or show the results. So I like using this method called select many, which is used to flatten this one to many relationship. Let me show you another example here, before I flattened it, is I'm going to write this query from r in regions, select r the…
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)
-