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:1
the execution will be deferred meaning they will have the same exec
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: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.
Source: stackoverflow.com
Related Query
- 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
More Query from same tag
- Handling null with dataset and LINQ
- Declaring an empty Queryable?
- Linq query with 'åäö' returns all items in table
- LINQ To SQL exception: Local sequence cannot be used in LINQ to SQL implementation
- Why does an IF Statement effect the outcome of my LINQ Statement?
- Can we aggregate and group-by in WPF with LINQ or something?
- LINQ to SQL in Visual Studio 2005
- Cannot implicitly convert type System.Collections.Generic.List
- Get data from three tables by ID C# APi
- How do I update a specific database entry with a view model as controller method attribute
- C# LINQ statement
- How to Sort a List of Objects by dictionary value?
- Can't cast IQueryable<Tobject> to Tobject
- How to Filter Child Collection from each Parent Entity in the Parent Collection using Linq
- Linq Lowest Unique Value or Lowest Row and Column Value Whichever Exact match or Partial Match Whichever is Value is smaller
- LEM2 algorithm implementation in C#
- Get child item using linq
- EFCore 3: How to compare string columns on server side
- IEnumerable<T>.ToLookup<TKey, TValue>
- Recurrence of events (LINQ query)
- Is there a more compact/better way to ZIP multiple lists using LINQ in C#?
- "Value cannot be null" while creating an XElement from the attributes where one or more value is null
- Group objects of same kind C#
- Deleting rows in a table with Linq to SQL
- Converting SQL to LINQ (left join on two fields)
- Ordering a list by size using Linq
- How to get exact hours and minutes from DbFunctions.DiffMinutes()
- Linq Group By - select a single record in each group into typed result
- The proper way to declare SQL Server varbinary field with LINQ
- C# linq totalization