Looping Through SubTables in a DataSet

G

Guest

My hope is that somebody has a thought on this, or can point to toward an
article that's useful. I have the following:

DataSet ds = new DataSet();
ds.ReadXml("MyData.xml");

Each MainTable entry has subtable relations. Here's my current approach:

foreach(DataRow row in ds.Tables["MainTable"].Rows
{
Console.WriteLine("Name = {0}", row["Name"].ToString());
...
foreach(DataRow subrow in row.GetChildRows(MyCurrentRelation)
{
}
}

This approach requires me to loop through the DataSet before going to the
code above, because GetChildRows requires me to know all the
MyCurrentRelation. I can't believe I have to loop through the dataset twice.
This seems way too cheezy.

Any pointers on looping through a table with related tables in a DataSet in
one nested loop sequence?

Thanks,
 
N

Nicholas Paldino [.NET/C# MVP]

Randy,

Why not cycle through the Relations first, mapping them to the
parent/child tables (store info in a hashtable), and then as you cycle
through your tables/rows, you can check to see if there are relations for
the table you are on.

Hope this helps.
 
K

Kevin Yu [MSFT]

Hi Randy,

I agree with Nicholas, that we can go through the DataSet.Relations
collection first. Each item is a DataRelation object. You'll be able to
fine the parent/chile table with ParentTable/ChildTable property. Please
check the following link for more information.

http://msdn2.microsoft.com/en-us/library/system.data.datarelation_members.as
px

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top