score:1
The snippets you've given seem to be invalid. c_3
isn't defined in the scope of the Select
statement, so unless I've misunderstood something, this won't compile.
It seems as though you're trying to select the elements of collection_3
, but this is done implicitly by SelectMany
, and so the final Select
statements in both cases are redundant. Take them out, and the two queries are equivalent.
All you need is this:
var query = collection_1
.SelectMany(c_1 => c_1.collection_2)
.SelectMany(c_2 => c_2.collection_3);
Update: x => x
is the identity mapping, so Select(x => x)
is always redundant, regardless of the context. It just means "for every element in the sequence, select the element".
The second snippet is of course different, and the SelectMany
and Select
statements indeed need to be nested in order to select all three elements, c_1
, c_2
, and c_3
.
Like Gert, says, though, you're probably better off using query comprehension syntax. It's much more succinct and makes it easier to mentally parse the workings of a query.
score:1
a. The queries are equal because in both cases you end up with all c_3
's in c_1
through c_2
.
b. You can't get to c_1
and c_2
with these queries as you suggest. If you want that you need this overload of SelectMany
. This "fluent" syntax is quite clumsy though. This is typically a case where comprehensive syntax which does the same is much better:
from c_1 in colection_1
from c_2 in c_1.collection_2
from c_3 in c_2.collection_3
select new { c_1.x, c_2.y, c_3.z }
Source: stackoverflow.com
Related Articles
- Can these two LINQ queries be used interchangeably?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Can someone explain why these two linq queries return different results?
- How do I combine these two linq queries into a single query?
- What is the difference between these LINQ queries
- Is there any way to reduce duplication in these two linq queries
- LINQ Source Code Available
- Why do these three pieces of LINQ code produce different (or erroneous) results?
- creating Linq to sqlite dbml from DbLinq source code
- Does LINQ convert code to SQL queries
- How to generalize these linq queries
- Do these 2 LINQ join queries achieve the same thing
- Why do these two linq queries return different numbers of results?
- How do I combine these similar linq queries into one?
- source code for LINQ 101 samples
- Are these two linq queries of the same performance? And How to implement .Any in linq query?
- How can I combine these linq queries into one?
- Linq can these sub queries be optimised?
- Simplify the following code to a one-liner using Linq queries
- What is the event handler equivilent to the LINQ code used here
- How to write these linq queries in the most optimal way
- c# Linq or code to extract groups from a single list of source data
- Ternary operator in LINQ queries used for handle nullable DateTime throws the exception
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- Error: "The specified LINQ expression contains references to queries that are associated with different contexts"
- Can LINQ be used in PowerShell?
- What Sorting Algorithm Is Used By LINQ "OrderBy"?
- How are people unit testing code that uses Linq to SQL
- GroupBy with linq entity poco
- Using Linq to filter out certain Keys from a Dictionary and return a new dictionary2
- How can I update ObservableCollection by its ID using C#?
- DbSet not seeing LINQ definitions
- LINQ return type issues for sending eMail in C#
- Converting LINQ (OrderBy/ThenBy) Code to CompareTo
- How to loop through a child list of objects in Entity Framework 4.1 code first
- LINQ - Return data from list 2 occurrences
- Filtering list if date is null or not using Linq query
- Iterate through Linq to Entities results until condition is met
- Select by ID from a list
- What's the equivalent VB.NET syntax for anonymous types in a LINQ statement?
- need to construct a clean Linq query for Graph Data
- Linq Expressions and "Not Contains" query
- Using .Contains within a linq query returning a SystemException
- Read all nodes value in root with there attribute through linq
- How to declare a variable inside a IQueryable.Select Method?
- C# Filtering collections through extended functions
- Issues with LINQ statement with defining an element of a collection
- LINQ GroupBy x where there is more then one x and y is unique