Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LINQ in foreach loop declaration

Tags:

.net

foreach

linq

Is it bad practice to declare LINQ directly in a foreach loop declaration? In terms of performance or subtle behavioral differences.

For example:

foreach (string name in persons.Select(x => x.name))
{
    //Do something with name
}
like image 415
lintmouse Avatar asked Jun 03 '13 01:06

lintmouse


People also ask

How to use foreach with LINQ?

Convert a foreach loop to LINQ refactoringPlace your cursor in the foreach keyword. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Convert to LINQ or Convert to Linq (call form).

Is LINQ faster than foreach?

It is slightly slower LINQ syntax is typically less efficient than a foreach loop. It's good to be aware of any performance tradeoff that might occur when you use LINQ to improve the readability of your code. And if you'd like to measure the performance difference, you can use a tool like BenchmarkDotNet to do so.

Is LINQ foreach parallel?

The linq parallel foreach will partition your collection and work on it at the same time, which you also need to take into account making sure your this. Update can work with multiple users without messing up .

Can I use foreach in IEnumerable C#?

The IEnumerator interface provides iteration over a collection-type object in a class. The IEnumerable interface permits enumeration by using a foreach loop.


2 Answers

Nope. Nothing wrong with that. As long as the Linq expression is short and easily readable, I'd say that's the most important thing. Your example is a really good example of when such queries should be use that way.

If it get's much longer than that or if you use query syntax, however, I recommend splitting it into two lines like this:

var names = persons.Select(x => x.name).Blah().Blah().Blah();
foreach (string name in names)
{
    //Do something with name
}

or

var names = 
    from x in persons
    select x.name;
foreach (string name in names)
{
    //Do something with name
}
like image 149
p.s.w.g Avatar answered Sep 28 '22 18:09

p.s.w.g


Bad practice? Not at all.

Performance could be impacted though, depending on the situation. For example, this:

persons.Select(x => x.name).Select(x => x.name + " more");

Could perform better than this:

foreach(string name in persons.Select(x => x.name))
{
    someList.Add(name + " more");
}

...if you're using something like Entity Framework where the name + " more" would happen on the database side vs local memory in the first example.

The best practice in my opinion is to go with whatever is most readable, then if you experience performance issues you can profile/tweak. Sometimes its clearer to use LINQ to build the IEnumerable, but the foreach to do some trickier stuff. If the LINQ part gets too long though, I would go with:

var somethings = something.Where(x => x == 1).Where(...).GroupBy(...etc);
foreach(var something in somethings)
{
    // Do something
}
like image 33
Ocelot20 Avatar answered Sep 28 '22 18:09

Ocelot20