【发布时间】:2019-02-20 09:39:08
【问题描述】:
我如何从另一个List<Type3> 获得一个包含另一个List<Type2> 的List<Type1>?
情况如下:
我有一个List<DbStruncture>。每个条目都包含一个DatabaseStructure
public partial class DatabaseStructure
{
public string TableSchema { get; set; }
public string TableName { get; set; }
public string ColumnName { get; set; }
public bool? IsPrimaryKey { get; set; }
}
我也有
public class Table
{
public string Name { get; set; }
public string Schema { get; set; }
public List<Column> Columns { get; set; }
}
public class Column
{
public string Name { get; set; }
public bool? IsPrimaryKey { get; set; }
}
现在我想将 List<DatabaseStructure> 中的数据填充到 List<Table> 中,其中包括 List<Column> 以及 Columns 中的所有 Table。
我用 LINQ 试了一下,结果如下:
var query =
from t in result
group t.TableName by t.TableName
into tn
select new
{
Table = tn.Key,
Schema = from s in result where s.TableName == tn.Key select s.TableSchema.First(),
Columns = from c in result where c.TableName == tn.Key select new Column
{
Name = c.ColumnName,
IsPrimaryKey = c.IsPrimaryKey
}
};
我的解决方案的问题是,我的查询不是通用列表...
谁能指出我正确的方向? LINW 是正确的方法吗?如果是,我如何获得想要的结果?
提前致谢
【问题讨论】:
标签: c# .net list linq generics