简体   繁体   中英

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?

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

因为IQuaryable方法被实体框架翻译成SQL 。但是IEnumerable方法不是。所以如果你使用IEnumearable所有数据都将从DB获取,这并不总是你想要的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM