2

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?

ramin_rp
  • 321
  • 1
  • 4
  • 14
  • 3
    http://stackoverflow.com/a/2876655/4074984 – Ceisc Mar 03 '15 at 11:34
  • 1
    Cause when you call an extension method (or even instance method) on an object, and the object cannot provide that by itself, the run-time will look up parents one after another until find the method. In your case, the provided `IQueryable Where()` (or any others) method comes first. So it will be raised – amiry jd Mar 03 '15 at 11:41

2 Answers2

5

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...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184