Click here to Skip to main content
15,906,628 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I have a list of Items. I can either use a Linq query or for loop to get the data that is needed by me. But I wanted to know which is Best ?

I think Linq works as same way of creating a loop and parsing the values and extract the data that is given in the query. Correct me if I am wrong..
Posted

1 solution

LINQ. It actually depends on LINQ to what. If this is, for example, LINQ to SQL, your speculations about loop are not correct at all. Apparently, LINQ is translated to SQL query, which is passed to the server part, which is optimized for processing queries, so performance difference could be striking.

—SA
 
Share this answer
 
Comments
Prasaad SJ 3-Sep-13 5:36am    
Hi.. Thanks for the answer. But my question is I have a list, Say 100 items in the list. What is the advantage of using Linq query over the loop here?
Sergey Alexandrovich Kryukov 3-Sep-13 12:16pm    
You can time the operation. Even if your loop code takes the same time in this case, LINQ is better because it makes the code more elegant...

Also, I would pay attention not for performance of searching for anything in the list, but to general design of your code and the ways to avoid such search. Maybe you need some redundancy (performance is commonly gained by adding redundancy) and use associative containers (Dictionary, SortedDictionary, SortedList)...

—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900