Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ReSharper suggest I convert a for loop into a LINQ expression?

In Visual Studio Re-Sharper keeps recommending I convert a for loop to a linq expression but what is the reason for this?

Which is faster?

Here are some example loops where resharper suggests a linq conversion:

foreach (XmlNode legendEntryNode in _legendEntryNodes) {     var xmlElement = legendEntryNode["FeatureType"];      if (xmlElement == null || !xmlElement.InnerText.Equals(featuretype)) continue;      var xmlNodeList = legendEntryNode.SelectNodes("Themes/Theme");      if (xmlNodeList != null)      foreach (XmlNode themeNode in xmlNodeList)     {         var element = themeNode["Value"];          if (element == null || !element.InnerText.Equals(v)) continue;          var xmlElement1 = themeNode["Icon"];          if (xmlElement1 != null)         {             string iconname = "<ms:ICON>" + xmlElement1.InnerText + "</ms:ICON>";              var element1 = themeNode["Highlight"];              if (element1 != null)             {                 string highlightname = "<ms:HIGHLIGHT>" + element1.InnerText + "</ms:HIGHLIGHT>";                 gml = gml.Insert(c, iconname + highlightname);                  c += (iconname.Length + highlightname.Length);             }         }         break;     } } 

And this simpler example:

for (int i = 0; i < getPointsRequest.Attribs.Length; i++) {     string attribName = getPointsRequest.Attribs[i].AttributeName;      if (!String.IsNullOrEmpty(attribName))     {         sqlQuery += "<ms:" + attribName + ">||\"" + attribName + "\"||</ms:" + attribName + ">";     } } 
like image 923
CSharpened Avatar asked Feb 14 '12 09:02

CSharpened


People also ask

Is LINQ faster than for loop?

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.

What is expression in LINQ?

LINQ introduced the new type called Expression that represents strongly typed lambda expression. It means lambda expression can also be assigned to Expression<TDelegate> type. The . NET compiler converts the lambda expression which is assigned to Expression<TDelegate> into an Expression tree instead of executable code.

Is LINQ slower than for?

Yes, it's slower.


2 Answers

Speed is very often irrelevant in large portions of your code - you should write code the simplest way, and then measure it to make sure it's fast enough.

If your for loop is really just querying, then LINQ is absolutely a great way to end up with more readable code. It's not universally applicable, but it's something you should at least bear in mind frequently.

Quite often a for loop can be converted into a query to be evaluated lazily, and then a foreach loop which performs some action on each value returned by the query. That can help separate the two aspects, letting you focus on one at a time when reading the code. It's important to keep LINQ queries as queries though, rather than using side-effects within them - it's designed to have a functional approach, which really doesn't mix pleasantly with side-effects.

If you have some concrete examples, we could give more opinions about which loops would make sense to convert to use LINQ, and which wouldn't.

like image 141
Jon Skeet Avatar answered Sep 17 '22 17:09

Jon Skeet


No performance gain as such, but some benefits

  • Makes code more readable.
  • Reduces the number of lines.
  • Easy to maintain.
  • In some cases you don't require temporary variables, which you might require in for loop. Using Linq you can chain queries.

For more details you can refer:

  • LINQ query operators: lose that foreach already
  • The “Anti-For” Campaign
  • Life After Loops

Hope this helps you.

like image 42
Amar Palsapure Avatar answered Sep 19 '22 17:09

Amar Palsapure