In a call to 'join' on nullable and non-nullable ints, type inference failed.

c# entity-framework linq non-nullable nullable

Question

I'm attempting to create an inner join to a nullable field in my Linq. Employee and Department are related, and Department may or may not have an EmployeeID. What join would I use if I just wanted the records that satisfied the inner join (no result for null EmployeeIDs)?

var result = from emp in employees
             join dept in departments
             on new { Source = emp.EmployeeID }
             equals new { Source = dept.EmployeeID };

I've been given an exemption:

The type of one of the expressions in the join clause is incorrect. Type Inference failed in a call to 'join'.

Thanks

1
9
9/8/2011 12:29:23 PM

Accepted Answer

How about reversing the connect and adding a bitwhere inside of?

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 };
9
12/9/2013 1:44:08 PM

Popular Answer

Compare Int? and Int using the add.Value relating to the nullable property

var result = from emp in employees
             join dept in departments
             on new { Source = emp.EmployeeID }
             equals new { Source = dept.EmployeeID.Value };


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow