Union multiple number of lists in C#

Viewed 13820

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.

2 Answers
Related