如何将 IEnumerable 转换为 C# 中的自定义类型?
我正在使用扩展方法 OrderBy 和 ThenBy 在多个字段上对自定义集合进行排序。 这种排序不会影响集合,而是返回 IEnumberable。 我无法将 IEnumerable 结果转换为我的自定义集合。 是否有办法更改我的集合的顺序或将 IEnumerable 结果转换为我的自定义集合?
I am using extension methods OrderBy and ThenBy to sort my custom collection on multiple fields. This sort does not effect the collection but instead returns and IEnumberable. I am unable to cast the IEnumerable result to my custom collection. Is there anyway to change the order of my collection or convert the IEnumerable result to my custom collection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您的集合类型实现了
IList
(以便能够Add()
),您可以编写一个扩展方法:If your collection type implements
IList<T>
(to be able toAdd()
to it) you could write an extension method:不,没有。 当您使用查询运算符时,它不会使用原始集合的实例来生成枚举。 相反,它使用私有实现(可能是匿名的,也可能不是)来提供此功能。
如果您希望它出现在原始集合中,则应该在该类型上有一个构造函数,该构造函数采用
IEnumerable
(或您的集合存储的任何内容,如果它是特定的),然后将查询传递给构造函数。然后,您可以使用它为
IEnumerable
创建一个名为To
的扩展方法,该方法将采用IEnumerable
和然后将其传递给您类型的构造函数并返回它。No there isn't. When you use the query operators, it doesn't use instances of the original collection to generate the enumeration. Rather, it uses private implementations (possibly anonymous, possibly not) to provide this functionality.
If you want it in your original collection, you should have a constructor on the type which takes an
IEnumerable<T>
(or whatever your collection stores, if it is specific) and then pass the query to the constructor.You can then use this to create an extension method for
IEnumerable<T>
calledTo<YourCollectionType>
which would take theIEnumerable<T>
and then pass it to the constructor of your type and return that.不。您可以遵循 ToList 和 ToDictionary 扩展方法建立的模式 - 编写类似的扩展方法来从 IEnumerable 加载您自己的集合类型。
No. You could follow the pattern established by the ToList and ToDictionary extension methods - write a similar extension method to load up your own collection type from IEnumerable.
您可以编写自己的 IComparer 实例,以便能够对自定义收集进行排序。
您可以强力循环遍历 IEnumerable 并将每个项目转储回自定义集合的新实例中。
You could write your own instance of IComparer to be able to sort your custom collect.
Brute force you could loop through the IEnumerable and dump each item back into a new instance of your custom collection.
您可以使用 IEnumerable 初始化列表:
如果 items 的类型为 IEnumerable,您可以创建如下列表:
或者您可以简单地调用 items.ToList():
You can initialize a list with IEnumerable:
If items is of type IEnumerable, you can create a list like this:
or you could simply call items.ToList():
您的自定义集合实现了哪些接口?
如果它是 IList 那么你可以在 IEnumerable 上执行 .ToList()
如果它是 IDictionary 那么你可以在 IEnumerable 上执行 .ToDictionary()
等等...
使其成为对您来说更可行的东西。
如果没有相似之处,您可以将 IEnumerable 的扩展方法编写为 .ToMyCollection()。
What interfaces does your custom collection implement?
If its an IList then you'd be able to do .ToList() on the IEnumerable
If its an IDictionary then you'd be able to do .ToDictionary() on the IEnumerable
Etc..
to bring it to something more workable for you.
You could write an extension method for IEnumerable to .ToMyCollection() if nothing is similar.
枚举和集合是完全不同的东西。
枚举只允许枚举,而集合具有基于索引的访问器、计数属性等。 要创建集合,您实际上必须枚举 IEnumerable。 典型的集合,如 List将有一个接受 IEnumerable 的构造函数。 尽管如此,仍将执行枚举。
Enumerables and collections are fairly different beasts.
Enumerations only allow to, well, enumerate while collections have index-based accessors, Count-property and the like. To create a collection you will actually have to enumerate the IEnumerable. Typical Collections like List<T> will have a constructor that accept an IEnumerable. Nonetheless, the enumeration will be performed.
如果您查看 System.Linq.Enumerable 类,有一些扩展方法可以让您将 IEnumerable 类型转换为 Array、ToList
您可以遵循此方法并创建一个自定义扩展方法,该方法将转换为您的自定义集合
If you look in the System.Linq.Enumerable class there are a few extension methods that will aloow you to convert you IEnumerable type to either an Array, ToList
You could follow this approach and create a custom extension method that will convert to your custom collection
LINQ 很酷,因为它将像这样的源集合视为不可变的 - 它不会修改它们。 使用扩展方法只是按照您指定的备用顺序枚举它们 - 您不会以这种方式构建新的自定义列表。
我建议两个选项:
LINQ is cool because it treats the source-collections like this as immutable - it is not modifying them. Using the extension methods simply enumerates them in the alternate order you specify - you are not building a NEW custom list in this way.
I would suggest two options:
您真的需要自定义集合类型吗? 听起来你想要做的一切都是由现有的通用集合类型提供的,也许你只需要为它们添加一个扩展方法 or to 。
Do you really need a custom collection type at all? It sounds like everything you want to do with this is provided by the existing generic collection types, and maybe you just need to add an extension method or to for them.