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 Query
- Is there a linq-y way to union collection properties of a collection of objects?
- C#: Is there a LINQ way to create an array of objects given an array of constructor parameters?
- Sum a collection of objects which contain numeric properties only with LINQ
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Best way to assign a value to a property of all objects in a collection using LINQ
- EF: Is there way to force Linq using UNION instead of UNION ALL
- Is there a way to fill a collection using a LINQ expression?
- Conditional UNION in LINQ (or is there a better way than UNIONs)?
- Is there any elegant way in LINQ to bucket a collection into a set of lists based on a property
- Is there a way to orderBy two properties simultaneously using LINQ
- Is there a way to loop through properties of a list of objects using linq?
- Is there a better way to code this LINQ fragment?
- How to fill properties of objects using LINQ and return collection
- Is there a better way to achieve this with Linq or a better code
- Best way to obtain objects from a collection that can be cast to a specific type using Linq
- Is there a way I can do a foreach that contains code in a LINQ expression?
- Is there a native LINQ way to return a typed collection in this example?
- Is there a LINQ way of converting a generic list of objects to an X by Y matrix of the same type?
- Is there a way to create collection of objects from another in Java (LINQ-like)?
- 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
- Is there way to use Distinct in LINQ query syntax?
- Is there a good LINQ way to do a cartesian product?
- Is there a neater linq way to 'Union' a single item?
- Is there a way to get the difference between two sets of objects in c#
- Using LINQ to Objects to find items in one collection that do not match another
- Is there a better way of calling LINQ Any + NOT All?
- Is there a Way to use Linq to Oracle
- Is there a neat way of doing a ToList within a LINQ query using query syntax?
More Query from same tag
- split list to multiple list on property basis
- What will be optimized version of the given code?
- Select items by ID from another list of items - Local sequence cannot be used in LINQ to SQL
- Why time consuming at .ToList() in linq to entities?
- linq excluding one result set from another
- Group a List<object[]>
- Using LINQ to create an IEnumerable<> of delta values
- Conditional projection with LINQ to Entities
- Assigning Alternate Color to a List Elements
- Add a list to a list Linq C#
- Converting for-loop to a linq query
- How can I write this query in LINQ
- Where clause in LINQ - C#
- Can I get a property from a collection using Linq in a more elegant way than this?
- Linq to XML, only take elements that have a certain child element
- Get MethodInfo for Enumerable.First() vs MethodInfo for Enumerable.OfType()?
- Linq query with Groupby Select and foreach loop
- Using query results with joined parameters without anonymous type
- LINQ - how to create a list of distinct items
- 'LINQ.SalesTaxRate' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument
- Linq Query returning IQueryable<IEnumerable<User>> not IQueryable<User>
- LINQ with two From clauses
- C#: Mapping a Data Type
- How to get stored procedure return value using linq with C#?
- linq where statement subject to a checkbox value
- Adding FirstOrDefault to my query?
- Linq group by ID and then sum the ID itself (key)
- ASP.NET MVC Linq Join
- Return object from Stored procedure in Linq
- Unsuccessful Linq query to retrieve data of composite key table