score:13
Accepted answer
It's as simple as SelectMany:
Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.
var kids = people.SelectMany(p => p.Kids);
(If you want a List<Kid>
instead of an IEnumerable<Kid>
, just call .ToList()
on the result.)
score:4
You can use the SelectMany extension method
var kids = new List(people.SelectMany(person => person.Kids));
score:3
A "LINQ-non-lambda-style" version of SelectMany
:
var allKids =
from p in people
from k in p.Kids // secondary "from" makes SelectMany (aka flat map)
select k;
// Result from above is IEnumerable, to Force evaluation as a List:
List<Kid> allKidsList = allKids.ToList();
Source: stackoverflow.com
Related Articles
- Find all entities using lambda
- How do I find the text within a div in the source of a web page using C#
- How to find the difference of two LIST<object> in c# using lambda expression
- Find Count using lambda Expression
- FIND items in list using lambda expression
- How do I find max date for matching entities by entity.ids using linq
- How can I fetch child entities as DTO in parent using reusable queries/Expression's with EF code first?
- Query XML source in LinqPad using lambda syntax
- where can I find a good example of using linq & lambda expressions to generate dynamic where and orderby sql?
- How to write the same code using Lambda Expression
- How to find subsets with exact length using linq lambda syntax
- What is the best way to find length of split characters from the given string by using String.Split() Method or Linq Lambda Expression in C#
- Generic List Find using lambda expression in c#
- How I can improve the code using lambda expressions?
- How to find time difference in datetime records of the same day using Lambda expression
- The entity or complex type cannot be constructed in a LINQ to Entities query - using lambda expression
- C# and Linq to Entities using count in a where lambda statement
- Using EF 4.1 and LINQ to find unassociated m...n-related entities
- Using C# lambda to find not matching elements in collection
- Find the category which has 3 or more than 3 products data by using the linq and lambda query
- Convert string[] to int[] in one line of code using LINQ
- C# - code to order by a property using the property name as a string
- Using GroupBy, Count and Sum in LINQ Lambda Expressions
- Using LINQ to Update A Property in a List of Entities
- How to find item with max value using linq?
- Entity-framework code is slow when using Include() many times
- Can you create a simple 'EqualityComparer<T>' using a lambda expression
- LINQ: Get all selected values of a CheckBoxList using a Lambda expression
- Find a record in dbSet using Find without a primary key
- Simple Examples of joining 2 and 3 table using lambda expression
- Grouping by an unknown initial prefix
- LINQ how to use In with numbers?
- C# linq xml missing element gives throws exception
- How to optimize code that changes a value deeply nested in an object graph
- How to Filter Child Collection from each Parent Entity in the Parent Collection using Linq
- How to get the latest number using LINQ
- Coverting Guid to string
- dynamic Linq queries with Entity Framework
- how to convert list object to list string( getting error Unable to cast object of type)
- VB.NET LINQ Query - List rows rows in table1 where data has changed
- XDocument File Name
- Left outer join gives error message 'Unable to cast the type 'System.Nullable`1' to type 'System.Object'.'
- EF - Error in converting type Dataaccess.Employees to ViewModels.Employee
- LINQ group data into a list of objects containing a list + selecting specific original data
- Return Rows with Unique and Duplicate values from dataTable using LINQ
- Why is DebuggerStepThrough ignored on linq statements?
- Aggregate data in DataTable in time intervals (5 minutes)
- Grouping a list of list using linq
- remove a record with child
- Manipulate DateTime using Linq