.net LINQPad:如何使用元表类型获取表中的行数?
在LINQPad中,我试图打印数据库中的所有表,并在每个表中打印行数:.net LINQPad:如何使用元表类型获取表中的行数?,.net,linqpad,.net,Linqpad,在LINQPad中,我试图打印数据库中的所有表,并在每个表中打印行数: this.Mapping.GetTables().Select(o => new { TableName = o.TableName, RowCount = ? }) 如何计算行数?尝试以下方法: var list = this.Mapping.GetTables() .Select(o => new { TableName = o.TableName, Type_ = o.Row.Type, RowC
this.Mapping.GetTables().Select(o => new { TableName = o.TableName, RowCount = ? })
如何计算行数?尝试以下方法:
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 });
这不是LinqPad问题,而是Linq问题。但是让我给你一个很好的LinqPad提示。将.Dump()添加到您的查询中,看看您得到了什么-非常酷
this.Mapping.GetTables().Select(o => new { TableName = o.TableName, RowCount = ? }).Dump()
试试这个:
var list=this.Mapping.GetTables()中的from o
让rowCount=ExecuteQuery(“从“+o.TableName”中选择count(*)
选择新的{
TableName=o.TableName,
行计数=行计数
};
this.Mapping.GetTables().Select(o=>new{
TableName=o.TableName,
行数=(
(IEnumerable)this.GetType().GetProperty(
o、 TableName.Substring(1,o.TableName.Length-2)
).GetValue(此值为空))
.Count()
}).Dump();
非常感谢您展示了如何按名称查找表。我也需要做同样的事情,我只查找包含数据的表。我很快发现,您必须小心从这里提取的内容,因为数据嵌套很深。下面的查询在我的机器上运行不到一秒钟,数据库中大约有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();
var list=this.Mapping.GetTables()
.Select(o=>new{
TableName=o.TableName,
类型=o.RowType.Type,
IsEntity=o.RowType.IsEntity,
RowCount=this.GetTable(o.RowType.Type).Cast().Count(),
})
.Where(o=>o.IsEntity&&o.RowCount>0)
.ToList();
list.Dump();
或不使用映射:
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();
this.GetType().GetProperties()。其中(p=>p.DeclaringType==typeof(TypedDataContext))。选择(t=>new
{
TableName=t.Name,
RowCount=((IEnumerable)(this.GetType().GetProperty(t.Name).GetValue(this,null))).Count()
}).Dump();
这完全不好。为什么要将直接SQL语句发送到DB并绕过Linq查询中可用的所有强类型检查?他使用的是LinqPad,不是生产系统,而是沙箱。没关系。还是个坏建议。这是草率的编程,不能解决这个问题。如何在这里添加转储帮助?我知道Dump方法,知道它很酷,但是它与这个问题有什么关系?我不知道Dump在这里有什么帮助?我应该在哪里添加它来查看您正在谈论的内容?
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();