简体   繁体   English

与ICollection和IEnumerable一起使用<T>

[英]work with ICollection and IEnumerable<T>

I have some code expecting an ICollection . 我有一些期待ICollection代码。 Now I want to devide that collection into smaller chunks by grouping them based on a given attribute (all elements within my collection are instances of an abstract base-class, however for legacy code we do not use the generic version of that interface). 现在,我想通过基于给定的属性将这些集合分组来将其划分为较小的块(集合中的所有元素都是抽象基类的实例,但是对于遗留代码,我们不使用该接口的通用版本)。 So what I´d like to achieve is to obtain any enumerable list of collections (one for every group). 因此,我想要实现的是获取任何可枚举的集合列表(每组一个)。 To get this I planned to convert the ICollection to the generic IEnumerable<T> using the Cast<T> -method on ICollection , and afterwards build a dictionary from that IEnumerable<T> using a LINQ-query. 为了得到这个我计划将转换ICollection到通用IEnumerable<T>使用Cast<T>上-方法ICollection ,然后建造一个字典从IEnumerable<T>使用LINQ查询。 But I fell there might be some better approaches. 但是我认为可能会有更好的方法。 Do you have any? 你有什么?

public override void DoSomething(ICollection features)
{
    var grp = features.Cast<MyBaseClass>().GroupBy(x => x.table);
    foreach(var g in grp) base.DoSomething(g);
}

EDIT: Hence the base-method (surely) has same signatur (also expects an instance of ICollection ) how do I get the values of the groups back to the (non-generic) ICollection ? 编辑:因此,基本方法(肯定)具有相同的signatur(也需要ICollection的实例)如何将组的值返回到(非通用) ICollection

I don't think there is anything wrong with your approach, but there is one caveat: if the ICollection instance contains an element that is not of type MyBaseClass , you'll get a cast exception. 我不认为您的方法有什么问题,但有一个警告:如果ICollection实例包含的元素不是 MyBaseClass类型, MyBaseClass异常。 You can work around this using the OfType extension method, which checks if the element is actually of the type and if so, it casts it to the target type. 您可以使用OfType扩展方法来解决此问题,该方法将检查元素是否实际为类型,如果是,则将其强制转换为目标类型。 If the element is of a different type, it is just ignored and no exception is thrown. 如果元素的类型不同,则将其忽略并且不会引发异常。

public override void DoSomething(ICollection features)
{
    var grp = features.OfType<MyBaseClass>().GroupBy(x => x.table);
    foreach(var g in grp) base.DoSomething((ICollection)g.ToList());
}

You can see a working example here: https://dotnetfiddle.net/0VkedY 您可以在这里看到一个有效的示例: https : //dotnetfiddle.net/0VkedY

Of course, if your requirement is that all elements must be of a specific type, you should use Cast instead of OfType . 当然,如果您的要求是所有元素都必须为特定类型,则应使用Cast而不是OfType

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

相关问题 从IEnumerable转换 <IEnumerable<string> &gt;到ICollection <T> - Converting from IEnumerable<IEnumerable<string>> to ICollection<T> 无法转换ICollection <t> 到IEnumerable <t> - Can't convert ICollection<t> to IEnumerable<t> IEnumerable的具体实现 <T> 那不是ICollection <T> - concrete implemantation of IEnumerable<T> that is not ICollection<T> 如何实现ICollection <T> 在一个IEnumerable上 <T> - How to implement ICollection<T> on an IEnumerable<T> 无法从IEnumerable <T>转换为ICollection <T> - Cannot convert from an IEnumerable<T> to an ICollection<T> 为什么ICollection <T>实现IEnumerable <T>和IEnumerable - Why does ICollection<T> implement both IEnumerable<T> and IEnumerable 如何选择IEnumerable <T> 而不是列表 <T> 来自ICollection <T> ? - How to pick IEnumerable<T> instead of List<T> from an ICollection<T>? 如何添加IEnumerable <T> 到现有的ICollection <T> - How can I add an IEnumerable<T> to an existing ICollection<T> 创建IEnumerable <T>或ICollection <T>的最简单,最紧凑的方法是什么? - What is the easiest and most compact way to create a IEnumerable<T> or ICollection<T>? ICollection/IEnumerable 操作遇到问题 - 不会删除事件 - Having trouble with an ICollection/IEnumerable operation - won't remove an occurrence
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM