2013-09-30 69 views
3

我嘗試做以下連接:無效的匿名類型成員聲明中加入兩個DataTable

var collection = from t1 in dt1.AsEnumerable() 
       join t2 in dt2.AsEnumerable() 
       on new { 
        t1.["main_code"], 
        t1["year"]} 
        equals new { 
         t2["dep_code"], 
         t2["dep_year"] } 
         select new { 
          emp_name = t1["name"], 
          bonus_desc = t1["bonus_desc"], 
          dep_name = t2["dep_name"] }; 

,但我面對下面的生成錯誤!

無效的匿名類型成員聲明

+0

是您的表dt1中是否存在bonus_desc? – nrsharma

回答

4

匿名類型的名稱屬性join

var collection = from t1 in dt1.AsEnumerable() 
       join t2 in dt2.AsEnumerable() 
       on new { PropertyA= t1["main_code"], PropertyB=t1["year"]} equals new { PropertyA=t2["dep_code"], PropertyB=t2["dep_year"] } 
       select new { emp_name = t1["name"], bonus_desc = t1["bonus_desc"],dep_name = t2["dep_name"] }; 
4

你需要明確地命名的屬性,例如:

var collection = 
    from t1 in dt1.AsEnumerable() 
    join t2 in dt2.AsEnumerable() 
    on new { code = t1["main_code"], year = t1["year"] } equals 
     new { code = t2["dep_code"], year = t2["dep_year"] } 
    select new { emp_name = t1["name"], bonus_desc = t1["bonus_desc"] /* ... */}; 
3

使用:

var collection = from t1 in dt1.AsEnumerable() 
       join t2 in dt2.AsEnumerable() 
       on new { field1 = t1.["main_code"], field2 = t1["year"]} equals new { field1 = t2["dep_code"], field2 = t2["dep_year"] } 
       select new { emp_name = t1["name"], bonus_desc = t1["bonus_desc"],dep_name = t2["dep_name"] }; 
相關問題