【问题标题】:LINQ create generic List of nestet objectsLINQ 创建嵌套对象的通用列表
【发布时间】: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&lt;DatabaseStructure&gt; 中的数据填充到 List&lt;Table&gt; 中,其中包括 List&lt;Column&gt; 以及 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


    【解决方案1】:

    好的,我自己找到了答案。

    这里是:

    var query =
                (from t in result
                 group t.TableName by t.TableName
                into tn
                 select new Table
                 {
                     Name = 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
                                }).ToList()
                 });
    

    【讨论】:

      【解决方案2】:
      1. 前言:我更喜欢(并推荐)使用带有扩展方法语法的 Linq 而不是使用 from,group,into 关键字,因为它更具表现力,如果您需要执行更高级的 Linq 操作,您可以无论如何都需要使用扩展方法。
      2. 首先,您的输入是非规范化的(我假设运行SELECT ... FROM INFORMATION_SCHEMA.COLUMNS 的输出),其中每一行都包含重复的表信息,因此使用GroupBy 按表标识符将行分组在一起(不要忘记同时使用表架构表名来唯一标识一个表!)
      3. 然后将每个组 (IGrouping&lt;TKey: (TableSchema,TableName), TElement: DatabaseStructure&gt;) 转换为 Table 对象。
      4. 然后通过执行来自IGrouping 组的内部Select 填充Table.Columns 列表,然后执行.ToList() 以获取具体的List&lt;Column&gt; 对象。

      我的表情:

      List<DatabaseStructure> input = ...
      
      List<Table> tables = input
          .GroupBy( dbs => new { dbs.TableSchema, dbs.TableName } )
          .Select( grp => new Table()
          {
              Name = grp.Key.TableName,
              Schema = grp.Key.TableSchema,
              Columns = grp
                  .Select( col => new Column()
                  {
                      Name = col.Name,
                      IsPrimaryKey = col.IsPrimaryKey
                  } )
                  .ToList()
          } )
          .ToList()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多