C# – The type of one of the expressions int he join clause is incorrect. Type inference failed in the call to ‘join’

asp.netasp.net-mvc-4clinq

I'm currently trying to join a column from another table to an existing table and return it to the view in MVC 4

var q = from accCo in db.AccControls
        join accCom in db.AccCompanies
        on new { accCo.ControlCode } equals
        new { ControlCode = accCom.Code }

        where accCo.ControlCode == Request.QueryString["ControlCode"]
        orderby accCom.Code
        select new Combined{ AccControls = accCo, AccCompoanies = accCom };

return View(q);

But I'm getting the above error

Best Answer

If any of the columns you join on is nullable, you will need to change it to .Value

on new { accCo.ControlCode } equals
            new { ControlCode = accCom.Code.Value }

or you can use this

var q = from accCo in db.AccControls
    join accCom in db.AccCompanies
    where accCom.Code != null
    on new { accCo.ControlCode } equals
    new { ControlCode = accCom.Code }

    where accCo.ControlCode == Request.QueryString["ControlCode"]
    orderby accCom.Code
    select new Combined{ AccControls = accCo, AccCompoanies = accCom };