3

我尝试执行以下加入:

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"] };

但我面临以下构建错误!

无效的匿名类型成员声明符

4

3 回答 3

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"] };
于 2013-09-30T11:41:01.240 回答
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"] /* ... */};
于 2013-09-30T11:41:52.990 回答
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"] };
于 2013-09-30T11:42:01.573 回答