列表,IList的,IEnumerable的,IQueryable的,ICollection的,这是最灵活的返回类型?这是、灵活、类型、列表

2023-09-02 21:32:15 作者:哥的潮流、谁能跟上节奏

我在这里看到了这个问题贴pviously $ P $但我并不满足,我明白了完整的后果。问题是返回类型应该使用LINQ到SQL换取最大的灵活性和查询能力,数据层。这是我读过的/发现:

IEnumerable的是有限的,只允许读进操作。 IEnumerable的是最通用的。我发现的是IEnumerable的确实允许查询操作VS的扩展语法。

列表允许,因为插入操作的最大的灵活性。

集合应该是用来代替名单,使只读集合。

IQueryable的不应该被使用,它应该是拿来主义和关闭。 IQueryable的不返回一个列表,但生成数据库查询语法。

我觉得我对权衡更好的手感,但仍不能确定的几件事情:

为什么我选择了具体类型的接口的变种?即IList的或ICollection的VS列表或收藏。有什么好处,我会得到什么?

我看到,扩展业务工作,但将扩大查询语法的工作呢?

之前有人建议我用AsQueryable已()。但是,为什么我会做的数据库,如果我没有联系?看来扩展方法,无论工作。

解决方案

集合一般不用于DAL的回报是非常有用的,因为一家集不会隐保证秩序。这项目只是一个水桶。一个IList,而另一方面,也隐含保证秩序。因此,我们到了IEnumerable或IList的。接下来的问题是:是List对象的活?也就是说,它是连接到数据的支持,这样,当你的项目添加到IList中,这将反映在DB?对于LINQ到SQL,这种情况并非如此。相反,你应该实体附加到表。所以,除非你提供这个额外的布线,一个列表是多余的。坚持使用的IEnumerable

I've seen this question posted here previously but I'm not satisfied that I understand the complete ramifications. The problem is what return type should a data layer that uses linq-to-sql return for maximum flexibility and query ability. This is what I've read/found:

IEnumerable is limited and only allows for read forward operation. IEnumerable is the most generic. What I've found is that IEnumerable does allow query operations vs the extension syntax.

C 学习笔记 集合

List allows for most flexibility because of insert operations.

Collections should be used instead of list to enable read only collections.

IQueryable should never be used, it should be "used and turned off". IQueryable doesn't return a list but generates a query syntax for database.

I feel I have a better feel for the trade offs but still not sure about a few things:

Why would I choose the interface variants over the concrete types? I.e IList or ICollection vs List or Collection. What benefit would I get?

I see that the extension operations work but will the expanded query syntax work as well?

Someone suggested I use AsQueryable() before. But, why would I do this if I don't have connection to the database? It seems the extension methods work regardless.

解决方案

Collections are not generally very useful for DAL returns, because a collection does not implicitly guarantee order. It's just a bucket of items. An IList, on the other hand, does implicitly guarantee order. So we're down to IEnumerable or IList. The next question would be: is the List object "live"? i.e., is it connected to the data backing so that when you add an item to the IList, it will be reflected in the DB? For LINQ-to-SQL, this is not the case. Rather, you're supposed to attach entities to the tables. So unless you supply this additional wiring, a List is superfluous. Stick with IEnumerable.

 
精彩推荐