Type Inference failed in a call to 'join' on nullable and non-nullable int

后端 未结 6 1872
半阙折子戏
半阙折子戏 2021-01-12 02:20

In my Linq, I am trying to make an inner join to a nullable field. Employee and Department have a relation, Department may have an EmployeeID or may have a null. So what wou

相关标签:
6条回答
  • 2021-01-12 02:45

    Check the type on emp.EmployeeID and dept.EmployeeID. You might be missing a cast if they are different.

    something like:

    on new { Source = emp.EmployeeID }
    equals new { Source = **(int)**dept.EmployeeID };
    

    Looks like emp.EmployeeID is of type int and dept.EmployeeID is of type nullable<int>.

    0 讨论(0)
  • 2021-01-12 02:47

    In my scenario I had this error on a join using multiple columns. The property names were different and one of them was also nullable. All I did was creating a name to those properties and adding the ".Value" on the nullable value so the LINQ Join could correctly associate those properties.

    var query = from y in Context.Table1
            join y in Context.Table2 on new { Field1 = x.Field1.Value, Field2 = x.Field2 }
            equals new { Field1 = y.Field1DiffName, Field2 = y.Field2 }
    

    I hope it helps whoever is facing this issue.

    0 讨论(0)
  • 2021-01-12 02:48

    What if you reverse your join and put a little where in there?

    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 };
    
    0 讨论(0)
  • 2021-01-12 02:49

    Found a useful answer from another link at https://social.msdn.microsoft.com/Forums/en-US/bf98ec7a-cb80-4901-8eb2-3aa6636a4fde/linq-join-error-the-type-of-one-of-the-expressions-in-the-join-clause-is-incorrect-type-inference?forum=linqprojectgeneral

    To join multi-valued keys you need to construct an anonymous typ on both sides of the 'equals' that is the same type. The anonymous type initializer expression infers both type and name of members from the expression you supply. In your case the names of the members are different so the types end up being different so C# cannot figure out the common type between the two.

    on new { VC.Make, VC.Model } equals new { MD.MakeID, MD.RefNum }

    should be

    on new { VC.Make, CV.Model } equals new { Make = MD.MakeID, Model = MD.RefNum }

    Using the name = value syntax in the initializer you can specify the name the compiler uses when creating the type. If all members types & names are the same then the anonymous types are the same type.

    0 讨论(0)
  • 2021-01-12 02:52

    To compare Int? and Int, append .Value 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 };
    
    0 讨论(0)
  • 2021-01-12 03:06

    I had the same issue, where my charge_codes.CompanyId was nullable but my order_items.CompanyId was NOT nullable.

    So I had to get my charge codes into their own ananomous type and make it not be nullable.

    var chargeCodes = from s in db.Charge_Codes
    where s.CompanyID != null
    select new { CompanyID = (int)s.CompanyID, 
                 Charge_CodeID = s.Charge_CodeID, 
                 Revenue_Code_Id = (int)s.Revenue_CodeID, };
    
    
    
    //now my chargeCodes contains an anonymous with a non nullable CompanyID and 
    //a non nullable Revenue_CodeID 
    
    //use chargeCodes here
    var query = from oi in db.Order_Items
    join cc in chargeCodes on 
    new {oi.CompanyID, oi.Charge_CodeID} equals new {cc.CompanyID, cc.Charge_CodeID}
    
    0 讨论(0)
提交回复
热议问题