Click here to Skip to main content
14,659,434 members
Rate this:
Please Sign up or sign in to vote.
See more:
Want your views on whether LINQ provides better performance (compared to say loop constructs like foreach) or it's just a syntactic sugar feature of the framework. I've been using LINQ from quite some day and got conflicting views from my seniors on the topic.
If you can provide some insights.
Updated 11-Jun-11 20:45pm
Did you ask yourself a simple question: improved (or degrades) performance -- compared to what?
RakeshMeena 12-Jun-11 2:43am
My mistake. Edited the question.
Sergey Alexandrovich Kryukov 12-Jun-11 23:20pm
Thank you. Answered.

Rate this:
Please Sign up or sign in to vote.

Solution 1

You can easily compare performance for such simple cases yourself using the class System.Diagnostics.Stopwatch, which provides very good accuracy. My prediction: if you program you loops correctly, you won't find any significant difference if any at all. Let's check it up!

Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi Rakesh,

It depends. For ex: for a simple for-each loop, using LINQ is not necessary, instead it can give more overhead. But using for-each where you want to add additional filtering and grouping, then it will be slow, it would be better to use a LINQ query in such conditions.

That was about Linq to Objects. Considering the case where you want to use Linq to Sql, I would like to recommend this 5-part series article regarding the performance of Linq to SQL :[^].

About Linq to XML, I'm not sure about the performance but its very easy to create XmlElement compared to the functionality provided by XmlDocument class. Its also easy to traverse and filter through the XML, also it has support to XPath expression using the XPathNavigator.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100