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

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

问题描述

AsQueryable已(),所以您可以围绕的IEnumerable 传递到所期望的方法<目的code>的IQueryable ,或者是有重新present一个有用的理由的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 版本,这些只是建立了一个防爆pression是最终做同样的事情,一旦它执行的扩展方法?我的主要问题是,什么真正的用例使用 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. 正如在其他的答案,你可以用它使用一个内存中的数据源嘲笑可查询的数据源,这样就可以更方便地测试方法,最终将在一个不可枚举基于<$使用C $ C>的IQueryable 。

您可以编写辅助方法来操作的集合,可应用于无论是在内存中的序列或外部数据源。如果你写你的帮助方法,使用的IQueryable 完全可以只使用 AsQueryable已上的所有可枚举使用它们。这可以让你避免编写两个独立的非常广义的helper方法的版本。

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 ,而不是一些派生类型。有效;你会在一个<$ C $上的IQueryable 使用它,你会使用相同的时间 AsEnumerable C>的IEnumerable 。你可能有一个对象,它实现的IQueryable 但也有一个实例选择方法。如果是这样的话,你想用LINQ 选择方法,你需要改变对象的编译时类型为的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天全站免登陆