日期:2014-05-20  浏览次数:20984 次

linq datatable join合并返回数据
DataTable dt1 = new DataTable();
  dt1.Columns.Add("c1");
  dt1.Columns.Add("c2");
  DataRow dr1 = dt1.NewRow();
  dr1["c1"] = "a";
  dr1["c2"] = "ad";
  dt1.Rows.Add(dr1);

  DataTable dt2 = new DataTable();
  dt2.Columns.Add("c3");
  dt2.Columns.Add("c4");
  DataRow dr2 = dt2.NewRow();
  dr2["c3"] = "a";
  dr2["c4"] = "b";


  DataRow dr3 = dt2.NewRow();
  dr3["c3"] = "a";
  dr3["c4"] = "c";

  dt2.Rows.Add(dr2);
  dt2.Rows.Add(dr3);

也就是相当于内联接,根据第一个表的c1等于第二个表的c3,查寻出两个表的所有内容
谢谢了.没分了...急啊


------解决方案--------------------
C# code

void Main()
{
    DataTable dt1 = new DataTable();
  dt1.Columns.Add("c1");
  dt1.Columns.Add("c2");
  DataRow dr1 = dt1.NewRow();
  dr1["c1"] = "a";
  dr1["c2"] = "ad";
  dt1.Rows.Add(dr1);

  DataTable dt2 = new DataTable();
  dt2.Columns.Add("c3");
  dt2.Columns.Add("c4");
  DataRow dr2 = dt2.NewRow();
  dr2["c3"] = "a";
  dr2["c4"] = "b";


  DataRow dr3 = dt2.NewRow();
  dr3["c3"] = "a";
  dr3["c4"] = "c";

  dt2.Rows.Add(dr2);
  dt2.Rows.Add(dr3);
  
  var query=from t1 in dt1.AsEnumerable()
            join t2 in dt2.AsEnumerable()
            on t1.Field<string>("c1") equals t2.Field<string>("c3") 
            select new {
            c1=t1.Field<string>("c1"),
            c2=t1.Field<string>("c2"),
            c4=t2.Field<string>("c4")
            };
  
 
}