AsQueryable() 的目的是什么? [英] What is the purpose of AsQueryable()?

查看:44
本文介绍了AsQueryable() 的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AsQueryable() 的目的是为了让您可以将 IEnumerable 传递给可能需要 IQueryable 的方法,还是有一个将 IEnumerable 表示为 IQueryable 的有用理由?例如,它是否应该用于这样的情况:

Is the purpose of AsQueryable() just so you can pass around an IEnumerable to methods that might expect IQueryable, or is there a useful reason to represent IEnumerable as IQueryable? For example, is it supposed to be for cases like this:

IEnumerable<Order> orders = orderRepo.GetAll();

// I don't want to create another method that works on IEnumerable,
// so I convert it here.
CountOrders(orders.AsQueryable());

public static int CountOrders(IQueryable<Order> ordersQuery)
{
    return ordersQuery.Count();
}

或者它真的让它做一些不同的事情:

Or does it actually make it do something different:

IEnumerable<Order> orders = orderRepo.GetAll();
IQueryable<Order> ordersQuery = orders.AsQueryable();

IEnumerable<Order> filteredOrders = orders.Where(o => o.CustomerId == 3);
IQueryable<Order> filteredOrdersQuery = ordersQuery.Where(o => o.CustomerId == 3);

// Are these executed in a different way?
int result1 = filteredOrders.Count();
int result2 = filteredOrdersQuery.Count();

这些扩展方法的 IQueryable 版本是否只是构建了一个表达式,一旦执行,它最终会做同样的事情?我的主要问题是,使用 AsQueryable 的真正用例是什么?

Do the IQueryable versions of these extension methods just build up an Expression that ends up doing the same thing once its executed? My main question is, whats a real use case for using AsQueryable?

推荐答案

有几个主要用途.

  1. 正如其他答案中提到的,您可以使用它来模拟使用内存数据源的可查询数据源,以便您可以更轻松地测试最终将用于基于不可枚举的IQueryable.

您可以编写辅助方法来操作可应用于内存中序列或外部数据源的集合.如果您编写的帮助方法完全使用 IQueryable,您只需在所有可枚举项上使用 AsQueryable 即可使用它们.这使您可以避免编写两个单独版本的非常通用的辅助方法.

You can write helper methods for manipulating collections that can apply to either in-memory sequences or external data sources. If you write your help methods to use IQueryable entirely you can just use AsQueryable on all enumerables to use them. This allows you to avoid writing two separate versions of very generalized helper methods.

它允许您将可查询的编译时类型更改为 IQueryable,而不是一些更派生的类型.有效;您可以在 IQueryable 上使用它,同时在 IEnumerable 上使用 AsEnumerable.您可能有一个实现 IQueryable 的对象,但它也有一个实例 Select 方法.如果是这种情况,并且您想使用 LINQ Select 方法,则需要将对象的编译时类型更改为 IQueryable.您可以直接转换它,但是通过使用 AsQueryable 方法,您可以利用类型推断.如果泛型参数列表很复杂,这会更方便,而且如果任何泛型参数是匿名类型,这实际上是必要.

It allows you to change the compile time type of a queryable to be an IQueryable, rather than some more derived type. In effect; you'd use it on an IQueryable at the same times that you'd use AsEnumerable on an IEnumerable. You might have an object that implements IQueryable but that also has an instance Select method. If that were the case, and you wanted to use the LINQ Select method, you'd need to change the compile time type of the object to IQueryable. You could just cast it, but by having an AsQueryable method you can take advantage of type inference. This is simply more convenient if the generic argument list is complex, and it is actually necessary if any of the generic arguments are anonymous types.

这篇关于AsQueryable() 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆