ramin_rp
ramin_rp

Reputation: 331

Why DbSet<t>.Where() used IQueryable version by default?

Whenever we want to use IEnumerable extension methods instead IQueryable version, we have use AsEnumerable() method.

var list = context.Details.AsEnumerable().Where(x => x.Code == "A1").ToList();

Why DbSet used IQueryable version of methods Like(Where , Select ,...) by default when other version available?

Upvotes: 2

Views: 650

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1503489

You'd usually want to use the IQueryable form, so that the filtering is done on the database instead of locally.

The code you've got will pull all records down to the client, and filter them there - that's extremely inefficient. You should only use AsEnumerable() when you're trying to do something that can't be performed in the database - usually after providing a server-side filter. (You may be able to do a coarse filter on the server, then a more fine-grained filter locally - but at least then you're not pulling all the records...)

Upvotes: 5

Selman Genç
Selman Genç

Reputation: 101731

Because IQuaryable methods are translated into SQL by Entity Framework.But IEnumerable method are not.So if you use IEnumearable all the data will be fetched from DB which is not always what you want.

Upvotes: 2

Related Questions