Click here to Skip to main content
15,891,889 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I need help in determining which is the better in fetching records in linq.
I have a production database where there s table let's say X which has almost 100,000 records. Now for searching just one record, I have two options one is using the dbContext and adding Where() or Find()...

C#
var dbSet = DataContext.Set<t>();

//1st way is Find() clause
X obj = dbSet.Find(id);

//2nd way is Where() clause
X obj = dbSet.Where(y); //where y is predicate like zz => zz.Id == id 


What I have tried:

Both give me the expected result but I wanted to know which is a better practice?
Posted
Updated 7-Dec-16 3:09am
Comments
Philippe Mori 7-Dec-16 10:22am    
The best practice is to test by yourself. That way, you will have a better understanding of the impact in your specific application. By the way, it is always a good idea to ensure that database is properly indexed and that relatively optimal queries are made. I often use LinqPad to test my query. You have to ensure that you user the same driver to access that data than the one used in the application.

after a short google..

this might be a good read.

c# - Find() vs. Where().FirstOrDefault() - Stack Overflow[^]
 
Share this answer
 
Comments
F-ES Sitecore 7-Dec-16 9:16am    
Interesting incredulous comments RE the performance, people often think that because something is "newer" it must be "faster". LINQ is there for convenience and simplicity of code, not for performance. If you want performance don't use LINQ.
F. Xaver 7-Dec-16 10:06am    
yeah. Some nice written LINQ one liner, slow as hell compared to a 10 liner For-loop iterating through the same collection.
If performance is necessary, better not use LINQ to much (or any)!
Anyway I rely like to use it when there is no need for performance/speed
Where : Instead of letting it find you can explicitly mention the required id.
 
Share this answer
 

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