score:12
Accepted answer
Use SelectMany
to flatten nested lists
asparaguses.AddRange(field.GetBeds().SelectMany(bed => bed.GetAsparaguses()));
score:6
Try SelectMany
in order to flatten the collection of collection:
asparaguses.AddRange(field
.GetBeds()
.SelectMany(bed => bed.GetAsparaguses()));
Source: stackoverflow.com
Related Articles
- Use Linq to flatten a nested list instead of a foreach loop
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- c# Linq or code to extract groups from a single list of source data
- why ForEach Linq Extension on List rather than on IEnumerable
- Updating list with linq instead of for each
- LINQ Advice - Using ToList().ForEach instead of 'classic' ForEach iteration
- LINQ Source Code Available
- ForEach for list of list in Linq
- C# Linq query help removing foreach loops creating cleaner code
- LINQ instead of foreach and list.addrange
- Why does c# Linq use reference instead of value in List containing List
- LINQ query returns old results when source list is re-initialized
- C# - Linq optimize code with List and Where clause
- convert foreach loop to linq code
- Linq Query Return List of Records instead of displaying single record
- Linq ForEach with AddRange and Contains And Select
- creating Linq to sqlite dbml from DbLinq source code
- LINQ expression instead of nested foreach loops
- nested foreach as LINQ with chained linked List
- Using a LINQ query instead of a foreach loop
- How to remove selected data that found on another list and take last 6 id through LINQ without foreach loop
- LINQ to Stored Procedure. Can I call the SP with a dynamic list of parameters instead of strongly typing them?
- Compare two list in C# and reject or select the child list based on conditions using LINQ or foreach
- Converting foreach loop to LINQ query breaks code
- Linq to format a list of items to replace foreach loop
- Is it faster to use LINQ to get a row from a List or a foreach loop
- Code practice to handle empty result set in Linq to custom list
- Want to use LINQ instead of foreach but it doesn't work
- Replacing foreach on a list with LINQ
- LINQ foreach during list creation
- Is there a better way to optimize the sql query using Linq?
- Unable to show View if Database table don't have records
- Get records from datatable grouped by date ( date interval)
- How do you project a generic list into a dictionary?
- .Range return true when it should be false
- Complex Xml namespaces in c#
- What's the difference between FindAll and Select?
- How to use a dictionary property in C# using LINQ
- Return a sql value indicating whether a file exists, using c# and ExecuteCommand
- C# Linq class searching - how to filter object collection
- Remove duplicates from Dataset but log the removed rows
- Handling 'Sequence has no elements' Exception
- Left join with OrderBy in LINQ
- Selecting/Filtering nested collections in L2E
- String's char value changed to Int
- Using Linq to order lists within a list
- Query unique year from MongoDB
- linq where clause and count result in null exception
- Remove items from xml with specific date through linq to xml in C#
- Testing for nested keys with Json.NET?