AsEnumerable() 与 "as IEnumerable<T>"
AsEnumerable() vs. "as IEnumerable<T>"
有什么区别
(db.Records as IEnumerable<Record>).Where(...)
和
(db.Records.AsEnumerable()).Where(...).
AsEnumerable()
只是 source as IEnumerable<T>
的快捷方式吗?
我读过 https://msdn.microsoft.com/en-us/library/bb335435(v=vs.90).aspx,但没有找到任何相关信息。
那篇文章有两个相关部分。
首先,
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.
其次,
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.
也就是说,是的,它看起来像一条捷径。但是,它旨在强制执行 Where
和其他 IEnumerable<T>
方法的内存操作,而不是基础类型的实际实现。
不同之处在于,您可以在 IEnumerable<T>
上调用 AsEnumerable
,其中 T
是匿名类型,并且您无法对此类序列执行此类转换,因为您无法键入没有名称的类型的名称。 AsEnumerable
这种推断通用参数的能力是它存在的原因。
(它也可以说是更简洁的语法,但在匿名类型之外这不是功能差异,只是个人偏好。)
有什么区别
(db.Records as IEnumerable<Record>).Where(...)
和
(db.Records.AsEnumerable()).Where(...).
AsEnumerable()
只是 source as IEnumerable<T>
的快捷方式吗?
我读过 https://msdn.microsoft.com/en-us/library/bb335435(v=vs.90).aspx,但没有找到任何相关信息。
那篇文章有两个相关部分。
首先,
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.
其次,
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.
也就是说,是的,它看起来像一条捷径。但是,它旨在强制执行 Where
和其他 IEnumerable<T>
方法的内存操作,而不是基础类型的实际实现。
不同之处在于,您可以在 IEnumerable<T>
上调用 AsEnumerable
,其中 T
是匿名类型,并且您无法对此类序列执行此类转换,因为您无法键入没有名称的类型的名称。 AsEnumerable
这种推断通用参数的能力是它存在的原因。
(它也可以说是更简洁的语法,但在匿名类型之外这不是功能差异,只是个人偏好。)