Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing performance of generated queries for Any() vs Count() in Entity Framework 4.1

I am working with Entity Framework 4.1 and C#.

Which one is the most suitable for best performance?

If so - why? (any links for additional readings) ?

bool isBoarding = invoice.Allocations.Where(a => a.Service.Key == "boarding").Count() > 0;

OR

bool isBoarding = invoice.Allocations.Any(a => a.Service.Key == "boarding");
like image 257
Sampath Avatar asked Nov 22 '12 11:11

Sampath


People also ask

Is Entity Framework 3 faster than Entity Framework 6?

The conclusions are obvious: in almost every test conducted by Chad, Entity Framework Core 3 is faster than Entity Framework 6 – exactly 2.25 to 4.15 times faster! So if performance is important to your application and it operates on large amounts of data, EF Core should be a natural choice. Is it faster than Dapper?

How can I improve the performance of queries in Entity Framework?

You can improve the overall performance of queries in the Entity Framework by using the following strategies. Generating views based on an entity model is a significant cost the first time that an application executes a query.

What are the operations of queries in Entity Framework?

In order to better understand the performance of queries in the Entity Framework, it is helpful to understand the operations that occur when a query executes against a conceptual model and returns data as objects. The following table describes this series of operations. Once in each application domain.

How to count the number of entities related to another entity?

var count = entities.Post.Where (p => p.SiteID == 1 && p.CreatedDate != null).Query ().Count (); Sometimes it is useful to know how many entities are related to another entity in the database without actually incurring the cost of loading all those entities. The Query method with the LINQ Count method can be used to do this. For example:


1 Answers

Count I believe will cause all records to be iterated over, whereas Any will stop at the first it finds.

EDIT: Just found an excellent post about count vs any take a look here

like image 59
Paul Zahra Avatar answered Sep 16 '22 13:09

Paul Zahra