Click here to Skip to main content
15,888,224 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why we use .ToList() method at the end of the LINQ select query? Can anyone explain its benefit and if do't use ToList() method then what problem may arise?
Posted

No issues if you dont use .ToList(). Its not compulsory to use .ToList(). Its only mandatory if you want to store the result into a List object.
Otherwise you can use 'var' keyword instead of .ToList() if you just simply want to enumerate.

Note: Using 'var' the comiler will automatically infer the return type from right side expression. You can hover your mouse on 'var' it shows you the exact type.

I hope you understand.
 
Share this answer
 
Comments
bbirajdar 1-Jun-13 5:55am    
+5... I suggest you to post the correct and exact to the point solutions like this in the future... :)
Mohammed Hameed 1-Jun-13 6:11am    
Thanks. Yes, I try my best.
Linq queries are ONLY executed when the data is needed. This gives the benefit of being to spread a single query into multiple queries without any loss in performance. When we call ToList(), the query is executed and the data is retrieved into a new List<type>. Without calling ToList(), it's just a query that'll execute when the data is needed. Now, think of a situation where you're using a Linq to SQL DataContext. You create the data context and build the query. Now, you dispose of the data context and THEN try to use the query. As the data context has been disposed, the query will fail and raise an exception. Now, say, before disposing the data context, you store the data in a List variable by calling ToList(). Then the data will have been retrieved and you can use that List. So, if at any time you think that the underlying data may change or won't be available when the data is going to be used, you can call ToList() and keep the List for later use.
 
Share this answer
 
Comments
Mohammed Hameed 1-Jun-13 5:31am    
I suggest you to go thru question one more time.
Nikunj6011 1-Jun-13 5:37am    
i am explaining for the use of Tolist() Method
Mohammed Hameed 1-Jun-13 5:47am    
Yes, I know. I suggest you to check Solution 4.
The ToList() method is used when your query can return more then 1 result, so that you can iterate through the results. If your query returns just 1 result you don't have to use ToList().

C#
// select item with given id
// query returns 1 result, you can catch it in a single object model.
ItemModel item = db.Items.Find(id);

// select all items
// query returns more then 1 result, you have to parse it to a List.
List<ItemModel> items = db.Items.ToList();
 
Share this answer
 
v3
Comments
bbirajdar 1-Jun-13 5:56am    
wrong answer.... .ToList() is used to convert the IQuerable /IEnumerable result to the generic List<> type ..It is a casting function
Sumon562 1-Jun-13 6:12am    
Thank you for your kind response. Can you explain me why should We convert IQuerable /IEnumerable result to the generic List<> type.
bbirajdar 1-Jun-13 6:46am    
Check solution 4 above.. Post a new question if you are not clear
bbirajdar 1-Jun-13 6:47am    
"Its only mandatory if you want to store the result into a List object."

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