简体   繁体   English

如何在IE#中将IEnumerable转换为自定义类型?

[英]How do I convert IEnumerable to a custom type in C#?

I am using extension methods OrderBy and ThenBy to sort my custom collection on multiple fields. 我使用扩展方法OrderBy和ThenBy在多个字段上对自定义集合进行排序。 This sort does not effect the collection but instead returns and IEnumberable. 这种排序不会影响集合,而是返回和IEnumberable。 I am unable to cast the IEnumerable result to my custom collection. 我无法将IEnumerable结果转换为我的自定义集合。 Is there anyway to change the order of my collection or convert the IEnumerable result to my custom collection? 反正有没有改变我的集合的顺序或将IEnumerable结果转换为我的自定义集合?

If your collection type implements IList<T> (to be able to Add() to it) you could write an extension method: 如果你的集合类型实现了IList<T> (能够对它Add() ),你可以编写一个扩展方法:

public static Extensions
{
    public static TColl ToTypedCollection<TColl, T>(this IEnumerable ien)
        where TColl : IList<T>, new()
    {
        TColl collection = new TColl();

        foreach (var item in ien)
        {
            collection.Add((T) item);
        }

        return collection;
    }
}

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. 如果你想在原始集合中使用它,你应该在类型上有一个构造函数,它带有一个IEnumerable<T> (或者你的集合存储,如果它是特定的),然后将查询传递给构造函数。

You can then use this to create an extension method for IEnumerable<T> called To<YourCollectionType> which would take the IEnumerable<T> and then pass it to the constructor of your type and return that. 然后,您可以使用此方法为IEnumerable<T>创建一个名为To<YourCollectionType>的扩展方法,该方法将获取IEnumerable<T> ,然后将其传递给您的类型的构造函数并返回该方法。

不可以。您可以遵循ToList和ToDictionary扩展方法建立的模式 - 编写类似的扩展方法,从IEnumerable加载您自己的集合类型。

You could write your own instance of IComparer to be able to sort your custom collect. 您可以编写自己的IComparer实例,以便能够对自定义集合进行排序。

Brute force you could loop through the IEnumerable and dump each item back into a new instance of your custom collection. 蛮力你可以遍历IEnumerable并将每个项目转储回自定义集合的新实例。

You can initialize a list with IEnumerable: 您可以使用IEnumerable初始化列表:

If items is of type IEnumerable, you can create a list like this: 如果items的类型为IEnumerable,则可以创建如下列表:

var list = new List<string>(items);

or you could simply call items.ToList() : 或者你可以简单地调用items.ToList()

var list = items.ToList();

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.. 如果它是一个IList,那么你就可以在IEnumerable上做.ToList()如果它是一个IDictionary,那么你就可以在IEnumerable Etc上做.ToDictionary()。

to bring it to something more workable for you. 把它带给更适合你的东西。

You could write an extension method for IEnumerable to .ToMyCollection() if nothing is similar. 如果没有相似之处,你可以为IEnumerable编写一个扩展方法.ToMyCollection()。

Enumerables and collections are fairly different beasts. 可枚举物和收藏品是完全不同的野兽。

Enumerations only allow to, well, enumerate while collections have index-based accessors, Count-property and the like. 枚举只允许枚举,而集合具有基于索引的访问器,Count属性等。 To create a collection you will actually have to enumerate the IEnumerable. 要创建集合,您实际上必须枚举IEnumerable。 Typical Collections like List<T> will have a constructor that accept an IEnumerable. 像List <T>这样的典型集合将有一个接受IEnumerable的构造函数。 Nonetheless, the enumeration will be performed. 尽管如此,将进行枚举。

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 如果查看System.Linq.Enumerable类,有一些扩展方法可以让你将IEnumerable类型转换为Array,ToList

var myEnumerable = this.GetEnumerator();

var array = myEnumerable.ToArray();
var list myEnumerable.ToList();

You could follow this approach and create a custom extension method that will convert to your custom collection 您可以按照此方法创建一个自定义扩展方法,该方法将转换为您的自定义集合

public static CustomCollection<TSource> ToCustomCollection<TSource>(this IEnumerable<TSource> source);

LINQ is cool because it treats the source-collections like this as immutable - it is not modifying them. LINQ很酷,因为它将像这样的源集合视为不可变的 - 它不会修改它们。 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: 我建议两个选择:

  1. Build a new list yourself based on the results of your LINQ-ing 根据LINQ的结果自己构建一个新列表
  2. Implement sorting capability into your custom collection (or use a nice sortable generic collection as a base) so it can sort in-place. 将排序功能实现到您的自定义集合中(或使用一个漂亮的可排序泛型集合作为基础),以便它可以就地排序。

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. 听起来你想用它做的一切都是由现有的通用集合类型提供的,也许你只需要添加一个扩展方法或者为它们添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM