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

发布于 2024-09-14 09:04:54 字数 171 浏览 8 评论 0原文

在 LINQPad 中,我尝试打印数据库中的所有表以及每个表中的行数:

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

如何计算行数?

In LINQPad, I'm trying to print all tables in a database and row count in each of them:

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

How the row count can be calculated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

彡翼 2024-09-21 09:04:54

我需要做同样的事情,并且我只查看包含数据的表。我很快发现您必须小心从这里提取的内容,因为数据是深度嵌套的。下面的查询在我的机器上运行不到一秒,数据库中有大约 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();

I had a need to do the same thing and I was looking ONLY the tables that contained data. I found out pretty quickly that you have to be careful of what you pull back from here because the data is deeply nested. The query below took less than a second to run on my machine and I have approximately 266 tables (not including views) in the database.
I hope that this answers your question.

 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();
农村范ル 2024-09-21 09:04:54
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();

感谢Jason 的回答展示了如何按名称查找表。

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();

Kudos to this answer from Jason for showing how to look up a table by name.

余厌 2024-09-21 09:04:54

试试这个:

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 });

Try this:

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 });
薔薇婲 2024-09-21 09:04:54

试试这个:

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
  };

Try this:

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
  };
讽刺将军 2024-09-21 09:04:54

或者不使用映射:

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();

Or without using Mapping :

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();
风吹雨成花 2024-09-21 09:04:54

这不是 LinqPad 问题,而是 Linq 问题。但让我给你一个很好的 LinqPad 技巧。将 .Dump() 添加到您的查询中,看看您会得到什么 - 非常酷。

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

This is not a LinqPad issue, but rather a Linq issue. But let me give you a nice LinqPad tip. Add .Dump() to your query and see what you get - very cool.

this.Mapping.GetTables().Select(o => new { TableName = o.TableName, RowCount = ? }).Dump()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文