Im looking for otimization.
When I call Count method in Entity Framework, does it process all the columns or only one or what?
If you also have any official site talking about this, I would appreciate.
Thank you.
You can also write: int count = context. Blogs. Single(blog=> blog.Id = yourCriteriaId).
Step 1: Create an entity class which inherits “DbContext” class. Step 2: The following is the structure of the database with table and stored procedure. Step 3: Create a class to store the returned tabular value. Step 4: Create an object for the entity above and method to call a function.
I did some tests a while ago and found out that EF does a count on the server, it sends a query with a SELECT COUNT so it does not load all records for sure.
about the columns, if you are referring to the difference between COUNT(*)
or COUNT(Id)
or COUNT(1)
I have read somewhere a while ago that for SQL Server there is no difference, the COUNT(*)
is optimized as COUNT(1)
anyway.
you could read many articles online or question here on SO... not excatly 100% what you asked but similar topics on performances of EF and ORM...
How to COUNT rows within EntityFramework without loading contents?
http://ayende.com/blog/4387/what-happens-behind-the-scenes-nhibernate-linq-to-sql-entity-framework-scenario-analysis
How to optimize Entity Framework Queries
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With