C# · 12月 29, 2021

c# – 类型推断在对可空和不可空的int进行“join”的调用中失败

在我的 Linq中,我试图使一个内部连接到一个可空字段.员工和部门有关系,部门可能有一个员工身份证件或可能有一个零.那么,如果我只想要满足内部连接的记录(对于null EmployeeID,没有结果),那么我的加入是什么: var result = from emp in employees join dept in departments on new { Source = emp.EmployeeID } equals new { Source = dept.EmployeeID };

我有一个例外:

The type of one of the expressions in the join clause is incorrect.
Type Inference Failed in a call to ‘join’.

谢谢

解决方法 如果你扭转你的加入并把一点点放在那里呢? var result = from department in departments where department.EmployeeID != null join employee in employees on department.EmployeeID.Value equals employee.EmployeeID select new { employee,department };