Sampath
Sampath

Reputation: 65880

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");

Upvotes: 8

Views: 6991

Answers (1)

BLoB
BLoB

Reputation: 9725

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

Upvotes: 10

Related Questions