views:

74

answers:

1

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?

+5  A: 

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.

Adam Gritt
Thanks Adam Gritt
madan