针对数据集的 LINQ 查询
我想打印出类型化数据集中的表数量以及与每个表和关键字段 {primary,foreign} 关联的字段数量。如何使用 LINQ 获取这些信息?
I Want to print out the number of tables in a Typed dataset along with the number of fields associated with each table and the key fields { primary,foreign}. How to get this information using LINQ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使它是类型化数据集,您仍然可以使用 DataSet 的 DataTables 属性和 DataTable 的 DataColumns 属性。但是,要将 Linq 与这些一起使用,您必须在适当的属性上调用
.OfType()
或.OfType()
将其转换为 IEnumerable< ;>以便您可以对它们运行 Linq 查询。之后,您只需检查适当的属性即可找出主键或外键。我不确定最后一部分是否有这些值的属性,或者您是否必须检查数据关系来确定这一点。编辑:
实际上,DataTable 上有一个 PrimaryKey 属性,它将返回组成 PrimaryKey 的 DataColumn 数组。对于外键,我相信您必须深入研究 DataRelations 集合来确定哪些列是外键。
Even with it being a typed dataset you can still use the DataTables property of the DataSet and the DataColumns propety of the DataTable. However, to use Linq with these you have to call
.OfType<DataTable>()
or.OfType<DataColumn>()
on the appropriate property to turn it into an IEnumerable<> so that you can run Linq queries against them. After that you should just have to check the appropriate properties to find out what is a Primary Key or Foreign Key. I am not sure on the last part if there is a property for these values or if you have to check the Data Relations to determine this.EDIT:
There is actually a PrimaryKey property on the DataTable that will return an Array of the DataColumns which make up the PrimaryKey. For the foreign keys though I believe you have to dig into the DataRelations collection to determine what columns are foreign keys.