简体   繁体   English

如何查询DataSet并遍历结果?

[英]How to query a DataSet and iterate through the result?

I have a DataSet which contains two tables, Publication and Owner, which are linked on Publication ID. 我有一个DataSet,它包含两个表,Publication和Owner,它们在Publication ID上链接。 How do I query the dataset? 如何查询数据集? What I am trying to do is get all of the owners for a particular publication, and then I want to iterate over the resulting set, concatenate the owner names together and populate a label with the information... 我想要做的是获取特定出版物的所有所有者,然后我想迭代结果集,将所有者名称连接在一起并用信息填充标签......

But lets begin with, how do i query the dataset? 但是让我们开始,我如何查询数据集?

I also have a DataRelation, can I query that somehow to get the child rows for the current row? 我也有一个DataRelation,我可以以某种方式查询以获取当前行的子行吗?

Thanks. 谢谢。

ADO.NET supports two fundamental approaches for performing filtering and sorting of datasets: ADO.NET支持两种执行数据集过滤和排序的基本方法:

The DataTable Select Method - This method is overloaded to accept arguments to filter and sort data rows returning an array of DataRow objects. DataTable Select方法 - 重载此方法以接受参数,以过滤和排序返回DataRow对象数组的数据行。

The DataView object sort, filter and find methods - This object uses the same filter arguments supported by the Select method, but the DataView exposes structures that can be bound to data-aware controls. DataView对象的排序,过滤和查找方法 - 此对象使用Select方法支持的相同过滤器参数,但DataView公开可绑定到数据感知控件的结构。 See DataView.RowFilter 请参见DataView.RowFilter

Iterating over filtered rows is as easy as: 迭代过滤的行非常简单:

DataTable dt;
...
foreach (DataRow dr in dt.Select(filter))
{
    // ...
}

This article contains several examples: A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 1 本文包含几个示例: .NET DataTables,DataSet和DataGrids实用指南 - 第1部分

You could look into LINQ to Dataset , which allows you to perform queries against DataSets with multiple tables. 您可以查看LINQ to Dataset ,它允许您对具有多个表的DataSet执行查询。 You can perform joins between the tables on the appropriate columns amongst other things. 您可以在适当的列之间执行表之间的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM