【发布时间】:2016-02-17 23:10:46
【问题描述】:
我有以下属性:
public List<List<MyClass>> Items { get; set;}
这与ListViews ItemSource 绑定,即IEnumerable。
就是这个IEnumberableItemSource我现在正在尝试扁平化的属性。
我已设法将其转换为以下内容
this.ItemsSource.Cast<IList>().ToList();
因为以下转换引发了无效转换异常:
this.ItemsSource.Cast<List<object>>().ToList();
我现在希望将此列表扁平化为直接的对象列表。我看了this answer: Flatten List in LINQ 并做了:
this.ItemsSource.Cast<IList>().ToList().SelectMany(x => x);
但这会产生以下错误:
'List' 不包含 'SelectMany' 的定义,并且没有 扩展方法“SelectMany”接受类型的第一个参数 可以找到“列表”(您是否缺少 using 指令或 汇编参考?)
那我做错了什么?是否可以展平List<IList>()?
额外信息:
值得一提的是,我使用的是Xamarin,这段代码是我的PCL (portable class library) 的一部分,尽管我确信这不是原因。
在调查这里发生的事情时,我也尝试过:
List<string> s = new List<string>();
s.SelectMany(x => x);
我得到了错误:
方法 'Enumerable.SelectMany(IEnumerable, Func>)' 的类型参数 不能从用法中推断出来。尝试指定类型参数 明确的。
【问题讨论】:
-
我猜你需要
using System.Linq; -
我已经为
System.Linq命名空间添加了using语句 -
public Items List<List<MyClass>>() { get; set;}是什么语法... C# 6 中的新东西? -
@MiguelAngelo 不,只是一个错字。现已修复
-
如果你这样做:
this.ItemsSource.Cast<List<MyClass>>().SelectMany(x => x);可以吗?
标签: c# linq list portable-class-library