在C#中合并多个列表 [英] Union multiple number of lists in C#

查看:658
本文介绍了在C#中合并多个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为以下情况寻找理想的解决方案:

I am looking for a elegant solution for the following situation:

我有一个包含类似List的类

I have a class that contains a List like

class MyClass{ 
...
 public List<SomeOtherClass> SomeOtherClassList {get; set;}
...
}

第三个叫Model的类包含一个List<Myclass>,这是我正在extern上操作的那个.

A third class called Model holds a List<Myclass> which is the one I am operating on from extern.

现在,我想使用返回所有MyClass实例上所有唯一的SomeOtherClass实例的方法来扩展Model类.

Now I would like to extend the Model class with a method that returns all unique SomeOtherClass instances over all MyClass instances.

我知道有一个Union()方法,有了一个foreach循环,我可以轻松地解决此问题,而我确实做到了.但是,由于我是C#3 +所有功能的新手,所以我很好奇无论有没有Linq,如何都能更优雅地实现这一点.

I know that there is the Union() method and with a foreach loop I could solve this issue easily, which I actually did. However, since I am new to all the C#3+ features I am curious how this could be achieved more elegantly, with or without Linq.

我找到了一种对我来说似乎很笨拙的方法,但是它有效:

I have found an approach, that seems rather clumsy to me, but it works:

        List<SomeOtherClass> ret = new List<SomeOtherClass>();
        MyClassList.Select(b => b.SomeOtherClasses).ToList().ForEach(l => ret = ret.Union(l).ToList()); 
        return ret;

注意:b.SomeotherClasses属性返回一个List<SomeOtherClasses>.

这段代码距离完美还很遥远,而有些问题是由于我必须弄清楚什么是使用C#3的好风格而哪些不是.因此,我列出了有关该代码段的想法,希望得到一些评论.除此之外,我很高兴听到一些评论,说明如何进一步改进此代码.

This code is far away from being perfect and some questions arise from the fact that I have to figure out what is good style for working with C#3 and what not. So, I made a little list with thoughts about that snippet, which I would be glad to get a few comments about. Apart from that I'd be glad to hear some comments how to improve this code any further.

  • 临时列表ret可能是C#2中一种方法的一部分,但是我应该能够使用方法链来辞职,这是正确的吗?还是我错过了重点?
  • 真的需要使用中间的ToList()方法吗?我只想对选择的每个成员执行进一步的操作.
  • 这些ToList()操作的成本是多少?他们是好风格吗?必要吗?
  • The temporary list ret would have been part of an approach in C#2 maybe, but is it correct that I should be able to resign this list with using method chaining instead? Or am I missing the point?
  • Is it really required to use the intermediate ToList() method? All I want is to perform a further action each member of a selection.
  • What is the cost of those ToList() operations? Are they good style? Necessary?

谢谢.

推荐答案

您正在寻找SelectMany() + Distinct():

List<SomeOtherClass> ret =  MyClassList.SelectMany( x => x.SomeOtherClasses)
                                       .Distinct()
                                       .ToList();

SelectMany()会将列表列表"展平为一个列表,然后您可以在此枚举中挑选出不同的条目,而不必在各个子列表之间使用并集.

SelectMany() will flatten the "list of lists" into one list, then you can just pick out the distinct entries in this enumeration instead of using union between individual sub-lists.

通常,您将希望避免使用Linq带来的副作用,您的原始方法是滥用我修改后的ret,这不是查询的一部分.

In general you will want to avoid side effects with Linq, your original approach is kind of abusing this my modifying ret which is not part of the query.

ToList()是必需的,因为每个标准查询运算符都会返回一个新的枚举,并且不会修改现有的枚举,因此您必须将最终的枚举结果转换回一个列表. ToList()的成本是枚举的完整迭代,在大多数情况下,可以忽略不计.当然,如果您的班级可以使用IEnumerable<SomeOtherClass>代替,则完全不必转换为列表.

ToList() is required since each standard query operator returns a new enumeration and does not modify the existing enumeration, hence you have to convert the final enumeration result back to a list. The cost of ToList() is a full iteration of the enumeration which, in most cases, is negligible. Of course if your class could use an IEnumerable<SomeOtherClass> instead, you do not have to convert to a list at all.

这篇关于在C#中合并多个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆