开发者

Dynamic table names

开发者 https://www.devze.com 2023-03-05 00:29 出处:网络
I have a database that my program will query. it has 3 tables all with the same structure: table1, table2 table3

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 w开发者_如何学Gohen I was writing the code table4 did not exist it may get added.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消