简体   繁体   English

IQueryable或IList

[英]IQueryable or IList

I am querying a database and producing a list of object from it. 我正在查询数据库并从中生成一个对象列表。

Normally from my business layer I return this list of objects cached of type IList. 通常,从我的业务层,我返回此类型的IList缓存对象列表。 If I change this to return IQueryable, what is the advantage to such? 如果我改变这个以返回IQueryable,那么这样做有什么好处? What does IQueryable give me that IList does not? IQueryable给了我什么IList没有?

IQueryable is an interface that is used to query databases, and defines functionality that allows implementors to do neat things like deferred execution and construction of more optimized queries (via evaluating the expression tree). IQueryable是一个用于查询数据库的接口,它定义了允许实现者执行诸如延迟执行和构建更优化查询(通过评估表达式树)等整洁事物的功能。 IList gives you none of that. IList没有给你这一点。

Furthermore, IQueryable is (for the most part) a read-only interface. 此外,IQueryable(大多数情况下)是只读接口。 IList defines Add, Remove, and so on that permit the user to modify the collection. IList定义了“添加”,“删除”等,允许用户修改集合。 If that makes more sense for you semantically, then by all means use that and then use the IEnumerable Linq extensions to get most of the functionality of IQueryable. 如果这对你在语义上更有意义,那么一定要使用它,然后使用IEnumerable Linq扩展来获得IQueryable的大部分功能。

If you are going to use to query the database then I think IQueryable is more useful. 如果您打算使用查询数据库,那么我认为IQueryable更有用。 APrta from that, with IList you can add,remove, get specific item from list. APrta,使用IList,您可以添加,删除,从列表中获取特定项目。 It all depends on how you want to use it.. 这一切都取决于你想如何使用它..

You could return IEnumerable<T> and then cast to either to make it more flexible... But IQueryable<T> is better approach for execution where you would literally query the database. 您可以返回IEnumerable<T>然后强制转换为使其更灵活...但IQueryable<T>是更好的执行方法,您可以从字面上查询数据库。 If you're wanting the ability to manipulate collections, I would choose IList<T> 如果你想要操纵集合的能力,我会选择IList<T>

I like to use IEnumerable for all return types - and I implement them with List in the method that returns it if they are simple, or a more specialized concrete class if I need to. 我喜欢对所有返回类型使用IEnumerable - 我在方法中使用List实现它们,如果它们很简单,则返回它,或者如果需要,可以使用更专业的具体类。 The advantage is, I rarely, if ever need to change the client of the class implementing IEnumerable. 优点是,我很少,如果需要更改实现IEnumerable的类的客户端。 I don't care if it is a List or a Hash table. 我不在乎它是List还是Hash表。 With IList, you're stuck using a List. 使用IList,您将无法使用List。

However, if you're using LINQ-to-SQL, or you are working with large amounts of data in your class, IQueryable supports some methods IEnumerable does not. 但是,如果您正在使用LINQ-to-SQL,或者您正在使用类中的大量数据,则IQueryable支持IEnumerable不支持的某些方法。 IEnumerable is the base implementation of ILIst and IQueryable, so that's why I recommend using that as your return type from most functions IEnumerable是ILIst和IQueryable的基本实现,所以我建议使用它作为大多数函数的返回类型

Check out these articles for a good explanation of the difference between IQueryable and IEnumerable: 查看这些文章,以便更好地解释IQueryable和IEnumerable之间的区别:

Returning IEnumerable<T> vs. IQueryable<T> and What is the difference between IQueryable<T> and IEnumerable<T>? 返回IEnumerable <T>与IQueryable <T>并且IQueryable <T>和IEnumerable <T>之间有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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