Alex
Alex

Reputation: 1232

Can I access entire DataTable if all I have is a single DataRow?

DataRow contains a Table property, which seems to return the entire Table for which this row belongs.

I'd like to know if I can use that table safely, or if there are gotcha's. In http://msdn.microsoft.com/en-us/library/system.data.datarow.table.aspx documentation, it says "A DataRow does not necessarily belong to any table's collection of rows. This behavior occurs when the DataRow has been created but not added to the DataRowCollection.", but I know for a fact my row belongs to a table.

In terms of pointers, if each Row from DataTable points to original DataTable, than I'm good to go. Is that all 'Table' property does?

Just to explain why I'm trying to get entire Table based on a single DataRow: I'm using linq to join two (sometimes more) tables. I'd like to have a generic routine which takes the output of linq (var), and generate a single DataTable with all results. I had opened another question at stackoverflow (Join in LINQ that avoids explicitly naming properties in "new {}"?), but so far there doesn't seem to be a generic solution, so I'm trying to write one.

Upvotes: 1

Views: 102

Answers (2)

Jason Meckley
Jason Meckley

Reputation: 7591

if you know the row is part of table than yes you can access it without any problem. if the possibility exists where the row may not be associated to a table than check if the property is null.

if(row.Table == null)
{
}
else
{
}

Upvotes: 2

Steve Wellens
Steve Wellens

Reputation: 20640

As long as it's not null, you can use it freely.

Upvotes: 1

Related Questions