关于AsEnumerable()行为 [英] About AsEnumerable() behavior
问题描述
@model IEnumerable< MyClass>
.....
在服务器端,我有一个linq查询,我的数据库,使用EF:
public ActionResult Index()
{
var query = from t in context.MyClass
选择t;
//现在来问题
return View(query);
return View(query.AsEnumerable()); //有什么区别吗?
}
我认为AsEnumerable()不是必需的,因为查询将自动投射那么,AsEnumerable()是有用的,有人可以解释一下吗?
谢谢,对不起我的坏英语!
没有必要。您声明的查询产生一个序列,它实现了 IEnumerable
界面。
正如你将看到这里,选择
实现 IEnumerable
的类型的扩展方法,返回一个 IEnumerable
。 / p>
public static IEnumerable< TResult>选择< TSource,TResult>(
此IEnumerable< TSource>源,
Func< TSource,TResult>选择器)
您的查询
var query = from t in context.MyClass
select t ;
将被编译为
var query = context.MyClass.Select(x => x);
所以我指的是选择
方法。
现在使用 AsEnumerable()
除了将实现IEnumerable的类型的编译时类型更改为IEnumerable本身以外,AsEnumerable(IEnumerable)方法具有无效应。 >
另外
AsEnumerable(IEnumerable)可以用于在序列实现IEnumerable时在查询实现之间进行选择,但也可以使用不同的公共查询方法集。例如,给定一个实现IEnumerable的泛型类,并有自己的方法,如Where,Select和SelectMany,调用Where将调用表的public Where方法。表示数据库表的表类型可能具有Where方法,该方法将谓词参数作为表达式树,并将树转换为SQL以进行远程执行。如果不需要远程执行,例如因为谓词调用本地方法,则可以使用AsEnumerable方法来隐藏自定义方法,从而使标准查询运算符可用。
有关进一步的文档,请查看这里。
I have an ASP.NET MVC application. Let's suppose that I have this view:
@model IEnumerable<MyClass>
.....
On the server side, I have a linq query, to my database, using EF:
public ActionResult Index()
{
var query = from t in context.MyClass
select t;
//now comes the question
return View(query);
return View(query.AsEnumerable()); //is there any difference?
}
I think that the AsEnumerable() is not necessary because the query will automatically cast to it, so, can someone explain me when the AsEnumerable() is useful?
Thank you, and sorry about my bad english!
It is not necessary. The query you have declared results in a sequence, which implements the IEnumerable
interface.
As you will see here, the Select
extension method of types that implement the IEnumerable
, returns a IEnumerable
.
public static IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TResult> selector)
Your query
var query = from t in context.MyClass
select t;
will be compiled to
var query = context.MyClass.Select(x=>x);
hence I am refering to the Select
extension method.
Regarding now the use of AsEnumerable()
The AsEnumerable(IEnumerable) method has no effect other than to change the compile-time type of source from a type that implements IEnumerable to IEnumerable itself.
Also
AsEnumerable(IEnumerable) can be used to choose between query implementations when a sequence implements IEnumerable but also has a different set of public query methods available. For example, given a generic class Table that implements IEnumerable and has its own methods such as Where, Select, and SelectMany, a call to Where would invoke the public Where method of Table. A Table type that represents a database table could have a Where method that takes the predicate argument as an expression tree and converts the tree to SQL for remote execution. If remote execution is not desired, for example because the predicate invokes a local method, the AsEnumerable method can be used to hide the custom methods and instead make the standard query operators available.
For further documentation, please have a look here.
这篇关于关于AsEnumerable()行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!