C# Linq - 连接具有多个字段的表时出现问题 - 错误 CS1941

gil*_*rpa 2 c# linq entity-framework linq-to-sql

我有以下 Linq,我认为它在语法上是正确的。

 var result = from t1 in context.t1
              join t2 in context.t2
              on new { t1.field1, t1.field2 } equals new { t2.field1, t2.field2 }
              select new { t1, t2 };
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

CS1941 The type of one of the expressions in the join clause is incorrect.  Type inference failed in the call to 'Join'.
Run Code Online (Sandbox Code Playgroud)

经过检查数据库,我发现以下内容:

table1                  |   table2
                        |
field1      varchar(16) |   field1  varchar(50)
field2      varchar(30) |   field2  varchar(50)
Run Code Online (Sandbox Code Playgroud)

字段数据类型和长度应该匹配吗?

Yas*_*ngh 8

       Can you please try this .


       var result = from x in entity
         join y in entity2
         on new { X1= x.field1, X2= x.field2 } equals new { X1=y.field1, X2= y.field2 }
         select new 
         {
           /// Columns
          };
Run Code Online (Sandbox Code Playgroud)