2

我有 3 张桌子

t1==>

 t1.ID    t1.co1     t1.col2

 1       a         b              
 2       a         b   

t2==>

 t2.ID    t2.co1     t2.col2

  1       a         b              
  2       a         b     

t3==>

 t3.ID    t3.co1     t3.col2

 1       a         b              
 2       a         b         

我想使用 Linq 在所有三个表之间进行内部连接,并希望在第四个数据表中选择列。

等效的sql查询:

SELECT t1.ID,t2.col1,t3.col2 
  FROM t1
INNER JOIN t2 ON t1.ID=t2.ID
INNER JOIN t3 ON t1.ID=t3.ID

t4==>

 t1.ID    t2.co1     t3.col2

 1        a         b              
 2        a         b  
4

2 回答 2

3

像这样的东西

var Result = 
    from row1 in t1
    join row2 in t2 on row1.ID equals row2.ID
    join row3 in t3 on row1.ID equals row3.ID
    select new { ID = row1.ID, Col1 = row2.col1, Col2 = row3.col2 }

DataTable dt = Result.CopyToDataTable();
于 2013-02-22T10:22:08.377 回答
1

用于从此处的类型LoadDataRow()获取 DataTable 。否则。anonymousResult.CopyToDataTable()

//Get the column list as same as table1 to new datatable
DataTable table4 = table1.Clone();

var Result = 
  from x in t1.AsEnumerable() join 
       y in t2.AsEnumerable() on x.Field<int>("ID") equals y.Field<int>("ID") join 
       z in t3.AsEnumerable() on x.Field<int>("ID") equals z.Field<int>("ID")
  select new table4.LoadDataRow( 
       new object[] { 
              x.ID, 
              y.col1, 
              z.col2 
           }, false);
于 2013-02-22T10:37:08.257 回答