How to flatten the list of array in Lambda expression

0 votes
asked Jul 22, 2015 in C#.NET by Raja

I am writing a lambda expression to get all types of classes, which are implemented some specific interface types, from AppDomain.CurrentDomain.GetAssemblies(). I am using the below Lambda expression to find the result. But the problem is, I am getting a Collection of array like IEnumerable<Type[]>. But I need it like just IEnumberable. How do I flatten the this result.

var types = AppDomain.CurrentDomain.GetAssemblies().Where(t => t.GetType().GetInterfaces()
                .Any(type => type.IsGenericType && type.GetGenericTypeDefinition() == typeof (IMyInterface<,>)))
                .SelectMany(a => a.GetTypes());
Share

1 Answer

0 votes
answered Jul 22, 2015 by Aadhira (1,213 points)
selected Jul 22, 2015 by administrator
 
Best answer

Instead of using Select statement, use SelectMany(). That will flatten your result. Try below code.

var types = AppDomain.CurrentDomain.GetAssemblies().Where(t => t.GetType().GetInterfaces()
                .Any(type => type.IsGenericType && type.GetGenericTypeDefinition() == typeof (IMyInterface<,>)))
                .SelectMany(a => a.GetTypes());

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...