score:4
The statements are, in this case, semantically the same as Select
(and linq in general) uses deferred execution of delegates. It won't run any declared queries until the result is being materialised, and depending on how you write that query it will do it in proper sequence.
A very simple example to show that:
var list = new List<string>{"hello", "world", "example"};
Func<string, string> func = (s) => {
Console.WriteLine(s);
return s.ToUpper();
};
foreach(var item in list.Select(i => func(i)))
{
Console.WriteLine(item);
}
results in
hello
HELLO
world
WORLD
example
EXAMPLE
score:4
In your first example, _executeFunc(row)
will NOT be called first for each item in rows
before your foreach
loop begins. LINQ will defer execution. See This answer for more details.
The order of events will be:
- Evaluate the first item in
rows
- Call
executeFunc(row)
on that item - Call
RunStoredProcedure(result)
- Repeat with the next item in
rows
Now, if your code were something like this:
foreach (var result in rows.Select(row => _executeFunc(row)).ToList())
{
RunStoredProcedure(result)
}
Then it WOULD run the LINQ .Select
first for every item in rows
because the .ToList()
causes the collection to be enumerated.
score:2
Select
uses deferred execution. This means that it will, in order:
- take an item from
rows
- call
_executeFunc
on it - call
RunStoredProcedure
on the result of_executeFunc
And then it will do the same for the next item, until all the list has been processed.
score:3
In the top example, using Select
will project the rows, by yielding
them one by one.
So
foreach (var result in rows.Select(row => _executeFunc(row)))
is basically the same as
foreach(var row in rows)
Thus Select is doing something like this
for each row in source
result = _executeFunc(row)
yield result
That yield is passing each row back one by one (it's a bit more complicated than that, but this explanation should suffice for now).
If you did this instead
foreach (var result in rows.Select(row => _executeFunc(row)).ToList())
Calling ToList()
will return a List of rows immediately, and that means _executeFunc() will indeed be called for every row, before you've had a chance to call RunStoredProcedure()
.
Thus what Resharper is suggesting is valid. To be fair, I'm sure the Jetbrains devs know what they are doing :)
score:1
The execution will be deferred meaning they will have the same exec
Source: stackoverflow.com
Related Articles
- Resharper, linq within foreach loop
- convert foreach loop to linq code
- Converting foreach loop to LINQ query breaks code
- Convert if-else within a foreach loop to LINQ
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Convert the code from Foreach loop to Linq
- Use a foreach loop within a foreach loop in a linq query
- How to convert the following foreach loop to linq code format?
- Multi-line foreach loop in linq / lambda
- Why does ReSharper suggest I convert a for loop into a LINQ expression?
- Linq query built in foreach loop always takes parameter value from last iteration
- Replace foreach loop with linq
- How to get Resharper to convert back to a foreach loop
- Simplifying a foreach loop with LINQ (selecting two objects in each iteration)
- Using LINQ in foreach loop declaration
- LINQ Source Code Available
- Linq + foreach loop optimization
- Linq optimisation within a foreach
- Populating List<T> within a foreach loop after pattern matching
- Use Linq to flatten a nested list instead of a foreach loop
- Resharper Convert foreach to LINQ using extension methods
- Simplify conventional foreach nested loop using linq & lambda expression
- C# Linq query help removing foreach loops creating cleaner code
- Resharper conver to LINQ bug? Or is my code wrong
- How to convert this foreach loop into Linq code?
- How to convert foreach loop to a Linq query?
- Convert a for loop nested in a ForEach loop to LINQ
- Invoking HashSet.Add(item) in Linq vs foreach loop
- I want to convert this foreach loop to a LINQ statement
- How to loop within a Linq to XML statement
- Optimize queries for Union, Except, Join with LINQ and C#
- Filter LINQ to entities query based on 2 dimensional array
- SQL LIKE Equivalent in C#
- LINQ, select from multi table
- C# Linq query to find strings in an array that end with strings in another array
- How to use OR operator in LINQ WHERE statement
- trying to send email from database value
- Use LINQ to compare items in a list
- Grouping common data into new string with LINQ C#
- How to mix monadic constructs in a single Linq expression?
- Why linq extension methods return concrete type
- Split a comma separated string while removing whitespace and empty entries
- Linq - Group by and count over multiple fields (output on single total)
- Linq Where list Contains item in another list
- Filtering Comma Separated Data
- LINQ, iterators, selecting and projection
- Using IEnumerable<T> and IQueryable<T> in a generic repository
- group by clause in LINQ
- Using Linq with 2D array, Select not found
- Linq c# sum any objects from list > int