Union multiple number of lists in C#
c#, extension-methods, linq, list
Solution
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.
Problem
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.