代码之家  ›  专栏  ›  技术社区  ›  Kamarey

LINQPad:如何使用MetaTable类型获取表中的行数?

  •  3
  • Kamarey  · 技术社区  · 14 年前

    this.Mapping.GetTables().Select(o => new { TableName = o.TableName, RowCount = ? })
    

    如何计算行数?

    6 回复  |  直到 14 年前
        1
  •  3
  •   dacke.geo    10 年前

    我需要做同样的事情,我只查看包含数据的表。我很快发现,您必须小心从这里提取的内容,因为数据嵌套得很深。下面的查询在我的机器上运行不到一秒钟,数据库中大约有266个表(不包括视图)。 我希望这能回答你的问题。

     var list = this.Mapping.GetTables()    
         .Select(o => new { 
        TableName = o.TableName, 
        Type_ = o.RowType.Type, 
        IsEntity = o.RowType.IsEntity,
        RowCount = this.GetTable(o.RowType.Type).Cast<object>().Count(),
        })
        .Where (o => o.IsEntity && o.RowCount > 0)
        .ToList();
    
     list.Dump();
    
        2
  •  2
  •   Community CDub    7 年前
    this.Mapping.GetTables().Select(o => new { 
        TableName = o.TableName, 
        RowCount = (
            (IEnumerable<object>)this.GetType().GetProperty(
                o.TableName.Substring(1, o.TableName.Length-2)
        ).GetValue(this, null))
        .Count()
    }).Dump();
    

    荣誉 this answer from Jason

        3
  •  1
  •   Nix    14 年前

    试试这个:

    var list = this.Mapping.GetTables()
        .Select(o => new { TableName = o.TableName, Type_ = o.Row.Type, RowCount = 0 }).ToList();
    
    list.ForEach(x=>x.RowCount  = this.GetTable(x.Type_).Count);
    list.Select(o=> new { TableName = o.TableName, RowCount = 0 });
    
        4
  •  0
  •   bluee    12 年前

    var list = from o in this.Mapping.GetTables()
    let rowCount = ExecuteQuery<int>("select count(*) from "+ o.TableName).FirstOrDefault()
     select new {
        TableName = o.TableName,
            RowCount = rowCount
      };
    
        5
  •  0
  •   Arslan    10 年前

    或者不使用映射:

    this.GetType().GetProperties().Where(p => p.DeclaringType == typeof(TypedDataContext)).Select(t => new 
    { 
        TableName = t.Name,
        RowCount = ((IEnumerable<object>)(this.GetType().GetProperty(t.Name).GetValue(this, null))).Count()
    }).Dump();
    
        6
  •  -4
  •   Randy Minder    14 年前

    this.Mapping.GetTables().Select(o => new { TableName = o.TableName, RowCount = ? }).Dump()