C# - AsEnumerable 示例
AsEnumerable 的具体用途是什么?是否会将不可枚举集合更改为可枚举集合 收藏吗?请给我一个简单的例子。
What is the exact use of AsEnumerable? Will it change non-enumerable collection to enumerable
collection?.Please give me a simple example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
请考虑以下示例。我们有一个名为
MyList
的自定义List
MyList
有一个名为Where
的方法,即Enumerable.Where( )
完全相同的名称。当我使用它时,实际上我正在调用我的Where
版本,而不是Enumerable
的版本现在我如何使用
找到小于 5 的元素Enumerable
的Where
版本?答案是:使用
AsEnumerable()
方法,然后调用Where
这次结果包含小于 5 的元素列表
Consider the following example. we have a custom
List
calledMyList
MyList
has a method calledWhere
which isEnumerable.Where()
exact same name. when I use it, actually I am calling my version ofWhere
, notEnumerable
's versionNow how can I find the elements which are less than 5 with the
Enumerable
's version ofWhere
?The answer is: Use
AsEnumerable()
method and then callWhere
This time the result contains the list of elements that are less than 5
来自 MSDN 文档的“备注”部分:
From the "Remarks" section of the MSDN documentation:
如果你看一下反射器:
它基本上只是向下转换实现 IEnumerable 的东西。
If you take a look in reflector:
It basically does nothing more than down casting something that implements IEnumerable.
由于某种原因没有人提到这一点,但请注意
something.AsEnumerable()
相当于(IEnumerable) some
。不同之处在于强制转换需要显式指定元素的类型,这当然很不方便。对我来说,这是使用AsEnumerable()
而不是强制转换的主要原因。Nobody has mentioned this for some reason, but observe that
something.AsEnumerable()
is equivalent to(IEnumerable<TSomething>) something
. The difference is that the cast requires the type of the elements to be specified explicitly, which is, of course, inconvenient. For me, that's the main reason to useAsEnumerable()
instead of the cast.AsEnumerable() 将数组(或列表或集合)转换为 IEnumerable类型。的集合。
请参阅 http://msdn.microsoft.com/en-us/library/bb335435 .aspx 了解更多信息。
从上面的文章来看:
AsEnumerable() converts an array (or list, or collection) into an IEnumerable<T> of the collection.
See http://msdn.microsoft.com/en-us/library/bb335435.aspx for more information.
From the above article:
阅读完答案后,我想您仍然缺少一个实际的例子。
我用它来使我能够在数据表上使用 linq
After reading the answers, i guess you are still missing a practical example.
I use this to enable me to use linq on a datatable
AsEnumerable
只能用于可枚举集合。它只是将集合的类型更改为IEnumerable
以更轻松地访问 IEnumerable 扩展。AsEnumerable
can only be used on enumerable collections. It just changes the type of the collection toIEnumerable<T>
to access more easily the IEnumerable extensions.不,它不会将不可枚举集合更改为可枚举集合。它是什么将集合作为 IEnumerable 返回给您,以便您可以将其用作可枚举。这样您就可以将该对象与 IEnumerable 扩展结合使用并被视为这样。
No it doesn't change a non-enumerable collection to an enumerable one. What is does it return the collection back to you as an IEnumerable so that you can use it as an enumerable. That way you can use the object in conjunction with IEnumerable extensions and be treated as such.
下面的示例代码可以说明 LukeH 的正确解释。
第一个Where是
Queryable.Where
,它被翻译成sql并在数据库中运行(o.Customer没有加载到内存中)。第二个Where是
Enumerable.Where
,它使用我不想发送到数据库的实例来调用内存中方法。如果没有
AsEnumerable
方法,我必须这样写:或者
两者都不能很好地运行。
Here's example code which may illustrate LukeH's correct explanation.
The first Where is
Queryable.Where
, which is translated into sql and run in the database (o.Customer is not loaded into memory).The second Where is
Enumerable.Where
, which calls an in-memory method with an instance of something I don't want to send into the database.Without the
AsEnumerable
method, I'd have to write it like this:Or
Neither of which flow well at all.