【发布时间】:2019-02-17 21:42:37
【问题描述】:
我有两个班级:
public class Row : Dictionary<string,string> {}
public class Table : List<Row> {}
在应该返回 Table 类型的对象的方法中,我尝试使用 Where-Statement 过滤 Table 类型的对象过滤后返回这个对象。
Table table = new Table();
table = tableObject.Where(x => x.Value.Equals("")).ToList();
return table;
我的问题是生成的 IEnumerable 的演员表。
- 使用 (Table) 进行转换会引发 InvalidCastException
附加信息:无法将“System.Collections.Generic.List`1[Row]”类型的对象转换为“Table”类型。
- 使用 as Table 进行转换会导致 null 对象
如何从 IEnumerable 中返回 Table 类型的对象?
【问题讨论】:
-
您是否要在一个表中获取表列表或行列表? “tableObject”是什么类型?
-
您想获取至少有一行具有您在“Equals”中检查的值的所有表吗?
-
所以
tableObject不是List<Table>而是List<Row>? -
你确定从
List<Row>继承是个好主意吗? stackoverflow.com/questions/21692193/why-not-inherit-from-listt