我目前正在尝试将另一个表中的列连接到现有表,并将其返回到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);


但是我遇到了以上错误

最佳答案

如果您加入的任何列可为空,则需要将其更改为.Value

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


或者你可以用这个

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 };

09-26 16:30