C# IQueryable - 会添加不同的维度吗?

发布于 2024-08-15 23:09:17 字数 438 浏览 6 评论 0原文

只是我正在维护一个项目。它是用 C# 3.0 编写的。一些实现将集合返回为 IQueryable。

就像

List<BookData> data = new List<BookData>();
   ...

   data.Add(new BookData { ID = "P001", BookTitle = "C# in Depth" });
   data.Add(new BookData { ID = "P002", BookTitle = "F# in Depth" });

    public IQueryable GetBooks()
    {
        return data.AsQueryable();
    }

代码将返回集合列表。退货有什么特别之处 作为AsQueryable?

Just I am maintaining a project.It has been written in C# 3.0.Some Implementations return collection as IQueryable.

like

List<BookData> data = new List<BookData>();
   ...

   data.Add(new BookData { ID = "P001", BookTitle = "C# in Depth" });
   data.Add(new BookData { ID = "P002", BookTitle = "F# in Depth" });

    public IQueryable GetBooks()
    {
        return data.AsQueryable();
    }

The code would have return the collection list. What is the special in returning them
as AsQueryable ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你 2024-08-22 23:09:17

在不使用反射或转换返回对象的情况下,返回集合上唯一可用的方法是接口定义的方法。这是限制某些类型的集合访问的一种方法——例如,IQueryable 没有 add 方法。不幸的是,面对代码的“敌对”用户,这并不安全。如果您确实需要集合不可侵犯,更好的方法是返回集合的只读副本,而不是将实际集合强制转换为不同的接口。

请注意,我假设一个列表(如您的示例中所示)或其他实际本地实现 IQueryable 的类。在这种情况下,返回源对象。如果底层对象未实现 IQueryable,则返回一个 IQueryable,代理对底层 IEnumerable 的调用。

Without using reflection or casting the returned object, the only methods that are available on the returned collection are those defined by the interface. This would be one way of restricting some types of access to the collection -- for instance, IQueryable doesn't have an add method. Unfortunately this isn't safe in the face of a "hostile" user of your code. If you truly need the collection to be inviolable, a better way is to return a read-only copy of the collection rather than the actual collection cast as a different interface.

Note that I'm assuming a List, as in your example, or some other class that actually implements IQueryable natively. In this case the source object is returned. If the underlying object doesn't implement IQueryable then an IQueryable is returned that proxies the calls to the underlying IEnumerable.

梦里人 2024-08-22 23:09:17

在像 List 这样的普通旧集合上调用时,AsQueryable 并没有真正执行任何操作,因此答案可能取决于代码库的另一部分。例如,某人可能定义了 GetBooks 方法来获取 IQueryable,目的是 GetBooks 方法将通过 LINQ 提供程序在数据库中执行任何排序或过滤。但是您正在查看的代码将书籍集构造为集合(List),而不是查询。为了将结果传递给假设的 GetBooks 方法,必须将集合包装在 IQueryable 中,即使该包装器只是直接委托回 LINQ to Objects 方法(而不是将 GetBooks 操作转换为 SQL 查询)。

或者您正在查看的类可能实现了一个接口,其中您正在查看的方法被声明为返回 IQueryable (出于与上面类似的原因),因此您的代码必须包装List 以保持与接口签名兼容。

AsQueryable doesn't really do anything when invoked on a plain old collection like a List<T>, so the answer probably depends on another part of the codebase. For example, someone might have defined a GetBooks method to take an IQueryable<Book>, with the intent that the GetBooks method would perform any ordering or filtering in the database via a LINQ provider. But the code you're looking at constructs the set of books as a collection (a List<Book>), not a query. In order for the result to be passed the hypothetical GetBooks method, the collection has to be wrapped in an IQueryable<Book>, even though that wrapper is just going to delegate straight back to the LINQ to Objects methods (rather than translating the GetBooks operations to SQL queries).

Or perhaps the class you're looking at implements an interface where the method you're looking at is declared as returning IQueryable<Book> (for similar reasons to above), so your code is having to wrap the List<Book> to remain compatible with the interface signature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文