I have the following property:
public List<List<MyClass>> Items { get; set;}
This is bound to a ListViews
ItemSource
which is IEnumerable
.
It is this IEnumerable ItemSource
property that I am now trying to flatten.
I have managed to cast it to the following
this.ItemsSource.Cast<IList>().ToList();
because the following cast threw an "Invalid Cast Exception":
this.ItemsSource.Cast<List<object>>().ToList();
I am now looking to flatten this list to just a straight list of objects. I looked at this answer: Flatten List in LINQ and did:
this.ItemsSource.Cast<IList>().ToList().SelectMany(x => x);
But I get this error:
'List' does not contain a definition for 'SelectMany' and no extension method 'SelectMany' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference?)
So what am I doing wrong? Is it possible to flatten a List<IList>()
?
Extra Information:
It may be worth mentioning that I am using Xamarin
and this code is part of my PCL (portable class library)
although I'm sure this wont be the reason.
While investigating what's going on here I have also tried:
List<string> s = new List<string>();
s.SelectMany(x => x);
and I get the error:
The type arguments for method 'Enumerable.SelectMany<TSource, TResult>(IEnumerable, Func<TSource, IEnumerable>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
>() { get; set;}`... something new in C# 6?
– Miguel Angelo Feb 17 '16 at 14:27>().SelectMany(x => x);` can it work?
– Ian Feb 17 '16 at 14:31