Reputation: 9171
I have a database that my program will query.
it has 3 tables all with the same structure: table1, table2 table3
How can I write a linq query that will query each of these table, with my dynamically specifying the tablename?
In addition to this. This solution must work if additional tables are added to the database. So even though when I was writing the code table4 did not exist it may get added.
Upvotes: 1
Views: 355
Reputation: 1048
try this:
DataSet s = new DataSet ();
DataTable t1 = new DataTable ();
t1.Columns.Add ("A", typeof (int));
t1.Columns.Add ("B", typeof (string));
s.Tables.Add (t1);
t1.Rows.Add (1, "T1");
t1.Rows.Add (2, "T1");
DataTable t2 = new DataTable ();
t2.Columns.Add ("A", typeof (int));
t2.Columns.Add ("B", typeof (string));
s.Tables.Add (t2);
t2.Rows.Add (1, "T2");
t2.Rows.Add (2, "T2");
t2.Rows.Add (3, "T2");
var result = from t in s.Tables.OfType<DataTable> ()
from r in t.Rows.OfType<DataRow> ()
select r;
Upvotes: 1