3 回答

TA貢獻1827條經驗 獲得超9個贊
您可以使用擴展方法來完成這項工作:
static class Extensions
{
public static Table ToTable<T>(this IEnumerable<T> collection) where T: Row
{
Table table = new Table();
table.AddRange(collection);
return table;
}
}
現在您可以簡單地調用此方法:
table = tableObject.Where(x => x.Value.Equals("")).ToTable();
或者您可以直接執行此操作,因為您創建了一個空的Table:
Table table = new Table();
table.AddRange(tableObject.Where(x => x.Value.Equals("")));
return table;

TA貢獻1789條經驗 獲得超10個贊
我假設你tableObject是一個List<Row>. Every Tableis aList<Row>但不是every List<Row>is a Table,這就是強制轉換不起作用的原因。
聽起來提供構造函數將是一個明顯的解決方案:
public class Table : List<Row>
{
public Table(IEnumerable<Row> rows) : base(rows) {}
}
table = new Table(tableObject.Where(x => x.Value.Equals("")));

TA貢獻1796條經驗 獲得超10個贊
你應該這樣做:
public class Row{
//whatever you have inside it
public string MyValue{get;set;}
}
public class Table{
public List<Row> Rows{get;set;}
}
Table table = new Table();
//renaming tableObject to bigListOfRows
table.Rows = bigListOfRows.Where(x => x.MyValue.Equals("")).ToList();
- 3 回答
- 0 關注
- 399 瀏覽
添加回答
舉報