Union multiple number of lists in C#

别来无恙 提交于 2020-02-23 08:43:48

问题


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

I have a class that contains a List like

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

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

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

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;

Note: The b.SomeotherClasses property returns a List<SomeOtherClasses>.

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.

  • 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?

Thanks.


回答1:


You are looking for SelectMany() + Distinct() :

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

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.

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() 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.




回答2:


You should have a look at SelectMany. Something like this should generate your "flat" list:

MyClassList.SelectMany(b => b.SomeOtherClasses)

It will return a IEnumerable<SomeOtherClass> which you can filter/process further.



来源:https://stackoverflow.com/questions/6948587/union-multiple-number-of-lists-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!