简体   繁体   English

我如何展平列表 <IList> ()?

[英]How do I flatten a List<IList>()?

I have the following property: 我具有以下属性:

public List<List<MyClass>> Items { get; set;}

This is bound to a ListViews ItemSource which is IEnumerable . 这绑定到一个IEnumerableListViews ItemSource

It is this IEnumberable ItemSource property that I am now trying to flatten. 我现在正尝试扁平化 IEnumberable ItemSource 属性。

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: 我看了一下这个答案:LINQ中的Flatten List,并做了:

this.ItemsSource.Cast<IList>().ToList().SelectMany(x => x);

But this gives the following 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?) “列表”不包含“ SelectMany”的定义,找不到可以接受类型为“列表”的第一个参数的扩展方法“ SelectMany”(是否缺少using指令或程序集引用?)

So what am I doing wrong? 那我在做什么错? Is it possible to flatten a List<IList>() ? 可以展平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. 值得一提的是,我正在使用Xamarin并且此代码是我的PCL (portable class library)一部分,尽管我确定这不是原因。

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(IEnumerable, Func>)' cannot be inferred from the usage. 无法从用法中推断出方法'Enumerable.SelectMany(IEnumerable,Func>)'的类型参数。 Try specifying the type arguments explicitly. 尝试显式指定类型参数。

Cast it to the right type first, then you can use SelectMany : 首先将其转换为正确的类型,然后可以使用SelectMany

var source = (List<List<MyClass>>) this.ItemsSource;
IEnumerable<MyClass> items = source.SelectMany(list => list);

Try this: 尝试这个:

this.ItemsSource.Cast<IEnumerable>().SelectMany(sublist => sublist.Cast<object>()).ToList()

I tested this with this sample program, and it compiled, and run: 我使用此示例程序对此进行了测试,并对其进行编译并运行:

class Test
{
    public List<List<int>> Items { get; set; }

    public IEnumerable ItemsSource { get { return this.Items; } }

    public List<object> Flatten
    {
        get { return this.ItemsSource.Cast<IEnumerable>().SelectMany(sublist => sublist.Cast<object>()).ToList(); }
    }
}

static class Program
{
    static void Main()
    {
        var t = new Test();
        t.Items = new List<List<int>>
        {
            new List<int> { 1, 2 },
            new List<int> { 11, 12 }
        };

        var r = t.Flatten;
    }
}

I assumed that, even though you know the type of the items, you pretend not to know when using the flattened list... that is why it returns a list of objects instead of a list of ints. 我以为,即使您知道项目的类型,也假装不知道何时使用扁平化列表...这就是为什么它返回对象列表而不是整数列表的原因。

做就是了 -

var flattened = Items.SelectMany(a => a);

Ok so interresting point here. 好吧,这里很有趣。 This is an extension of @TimSchmelters answer explaining why casting to the right type matters 这是@TimSchmelters答案的扩展,解释了为什么转换为正确类型很重要

From my second error in the aside which was: 从我旁边的第二个错误是:

The type arguments for method 'Enumerable.SelectMany(IEnumerable, Func>)' cannot be inferred from the usage. 无法从用法中推断出方法'Enumerable.SelectMany(IEnumerable,Func>)'的类型参数。 Try specifying the type arguments explicitly. 尝试显式指定类型参数。

after some googling I found this answer to the question: "SelectMany() Cannot Infer Type Argument — Why Not?" 经过一番谷歌搜索后,我找到了这个问题的答案:“ SelectMany()无法推断类型参数—为什么不呢?”

This states this error occurs because of the following: 这表明由于以下原因而发生此错误:

The type returned by the delegate you pass to SelectMany must be an IEnumerable 您传递给SelectMany的委托返回的类型必须是IEnumerable

So the error boils down to how I am casting it: 因此错误归结为我如何投射它:

I must cast it to IEnumerable<T> and obviously IList isnt that. 我必须将其转换为IEnumerable<T> ,显然IList并非如此。 If I do the following however it now works: 如果我执行以下操作,则现在可以使用:

this.ItemsSource.Cast<IEnumerable<object>>().ToList().SelectMany(x => x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM