score:7
Accepted answer
Here's the obligatory method form.
return parents
.SelectMany(p => p.Children);
And for two levels:
return oldies
.SelectMany(grand => grand.Children)
.SelectMany(parent => parent.Children);
score:9
This will work:
public IEnumerable<Child> GetAllChildren(IEnumerable<Parent> parents)
{
return from parent in parents
from child in parent.Children
select child;
}
and then this:
public IEnumerable<Child> GetAllChildren(IEnumerable<Grandparent> nanas)
{
return from papa in nanas
from parent in papa.Children
from child in parent.Children
select child;
}
Note, in this example I'm not actually returning a list, I'm returning an IEnumerable data source that until you start to foreach over it, or similar, won't actually do any processing.
If you need to return a list, modify each return statement as follows:
return (from .....
...
select child).ToList();
Source: stackoverflow.com
Related Articles
- Sum a collection of objects which contain numeric properties only with LINQ
- Is there a linq-y way to union collection properties of a collection of objects?
- How to fill properties of objects using LINQ and return collection
- Merge a collection with duplicate objects and update some properties with LINQ
- Linq to convert collection of objects with xml properties to collection of objects representing xml elements?
- Update all objects in a collection using LINQ
- C#: Is there a LINQ way to create an array of objects given an array of constructor parameters?
- Using LINQ to Objects to find items in one collection that do not match another
- Is there a good source that gives an overview of linq optimizations?
- C# Linq - get objects with not empty collection
- LINQ WHERE method alters source collection
- is there anyway to remove from a collection based on a .Where() linq clause (.RemoveWhere() ?)
- Is there a LINQ extension or (a sensible/efficient set of LINQ entensions) that determine whether a collection has at least 'x' elements?
- using LINQ how can i concatenate string properties from itesm in a collection
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Need a linq to objects query for a nested collection
- LINQ query to create a collection of objects combined from a collection of strings
- Are there some disadvantages in using "a lot" of LINQ to Objects statements?
- LINQ Source Code Available
- Update all objects except one in a collection using Linq
- Best way to assign a value to a property of all objects in a collection using LINQ
- How to know whether there is any overlap in a collection of periods using LINQ
- LINQ Union between two tables with the same fields and then returned in a collection
- LINQ order a collection of objects by date in descending order
- LINQ query to detect duplicate properties in a list of objects
- EF: Is there way to force Linq using UNION instead of UNION ALL
- Is there a bug in this code from 101 LINQ Samples on MSDN? (Update: Fixed)
- Linq intersect or join to return items from one collection that have matching properties to another?
- LINQ to Entities null reference on Union with nested objects
- sorting list of objects with null properties using linq
- How can I do case insensitive and concatenated field searches using nhibernate / linq?
- Read an XML node in namespace using Linq
- C# Sorting IEnumerable of IEnumerables
- Does not contain definition for 'Contains'
- Join Multiple Datatables in LINQ using Group Join
- LinqDataSource Select syntax
- How can I select using LINQ for an entry that contains a LIST with more than one row?
- Error 'Newtonsoft.Json.Linq.JToken' cannot be serialized using TombstoneHelper
- How to call an Sql User defined Function using Entity frame work Code first approach with LInq c#
- EF Core Linq Query to Select Rows That Match From a List of Possibilities
- Group by element in Linq
- LINQ to Entities does not recognize the method 'Single ToSingle(System.String)' method, and this method cannot be translated into a store expression
- Linq equivalent for grouping select
- remove a few items (Id) from results
- The type of the arguments cannot be inferred usage Linq GroupJoin
- Send IQueryable of class object from view to controller class
- Linq join query from 2 different entity frameworks
- How to initialize IEnumerable<Object> that be empty and allow to Concat to it?
- LINQ, joining results of two methods
- LINQ select: want to pass collection into CONTAINS