为什么要使用.AsEnumerable(),而不是转换为IEnumerable的&LT; T&GT ;? [英] Why use .AsEnumerable() rather than casting to IEnumerable<T>?
问题描述
一对扩展方法的IEnumerable&LT; T&GT;
是 .AsEnumerable()
。这种方法,它被称为在枚举对象转换成的一个实例的IEnumerable&LT; T&GT;
。然而,由于对象必须实现的IEnumerable&LT; T&GT;
,以适用于本扩展方法,转化为的IEnumerable&LT; T&GT;
是铸造的IEnumerable℃的简单的事情; T&GT;
。我的问题是,为什么这种方法在所有存在?
One of the extension methods on IEnumerable<T>
is .AsEnumerable()
. This method converts the enumerable object it was called on into an instance of IEnumerable<T>
. However, since an object must implement IEnumerable<T>
in order to apply to this extension method, converting to IEnumerable<T>
is a simple matter of casting to IEnumerable<T>
. My question is why does this method exist at all?
例如:
List<string> strings = new List<string>() { "test", "test2", "test3" };
IEnumerable<string> stringsEnum1 = strings.AsEnumerable();
IEnumerable<string> stringsEnum2 = (IEnumerable<string>)strings;
在上面的例子中, stringsEnum1
和 stringsEnum2
是等价的。什么是扩展方法的点?
In the example above, stringsEnum1
and stringsEnum2
are equivalent. What's the point of the extension method?
修改:作为一个推论,为什么会出现铸造的IQueryable&LT时
相当于 .AsQueryable()
方法; T&GT ;
Edit: As a corollary, why is there an .AsQueryable()
method when casting to IQueryable<T>
is equivalent?
推荐答案
可读性是这里的主要问题。考虑到
Readability is the main issue here. Consider that
Table.AsEnumerable().Where(somePredicate)
是更具可读性比
((IEnumerable<TableObject>)Table).Where(somePredicate).
或者试想将执行SQL Server上的查询,并在内存中,其余的一部分:
Or imagine wanting to execute part of the query on the SQL Server and the rest in memory:
Table.Where(somePredicate)
.Select(someProjection)
.AsEnumerable()
.SomethingElse()
与
((IEnumerable<SomeProjectionType>)Table.Where(somePredicate)
.Select(someProjection))
.SomethingElse()
现在,至于为什么这样的方法是有用的都想到了一个表
在LINQ到SQL的例子的DataContext
。由于表
是的IQueryable
它实现的IEnumerable
。当你调用一个其中,
法这样的表
,并通过结果枚举,code被执行,最终会导致一个SQL Server上执行的SQL语句。什么 AsEnumerable
确实是说,不,我不希望使用LINQ到SQL供应商执行其中,
,我想使用LINQ到对象实施其中,
。
Now, as for why such a method is useful at all think of the example of a Table
in a LINQ to SQL DataContext
. As Table
is an IQueryable
it implements IEnumerable
. When you invoke a Where
method on such a Table
and enumerate through the results, code is executed that eventually causes a SQL statement to be executed on a SQL Server. What AsEnumerable
does is says, no, I don't want to use the LINQ to SQL provider to execute the Where
, I want to use the LINQ to Objects implementation of Where
.
因此在枚举
Table.Where(somePredicate)
会导致一个SQL Server,而在枚举上执行一个查询
causes a query to be executed on a SQL Server whereas enumerating over
Table.AsEnumerable().Where(somePredicate)
带来了由表
psented到内存表重新$ P $和执行内存中的其中,
功能(和没有在SQL Server上!)
brings the table represented by Table
into memory and executes the Where
functionality in memory (and not on the SQL Server!)
这是 AsEnumerable
点:允许您隐藏了具体的实施方法 IEnumerable和改用标准的执行情况。
This is the point of AsEnumerable
: to allow you to hide a specific implementation of IEnumerable
methods and instead use the standard implementation.
这篇关于为什么要使用.AsEnumerable(),而不是转换为IEnumerable的&LT; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!