我正在尝试将另一个表中的列连接到现有表并将其返回到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);
但我收到了上述错误
答案 0 :(得分:9)
如果您加入的任何列可以为空,则需要将其更改为.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 };
答案 1 :(得分:1)
尝试
var q = from accCom in db.AccCompanies
let accCo = db.AccControls.FirstOrDefault(x => x.ControlCode == accCom.Code)
where accCo.ControlCode == Request.QueryString["ControlCode"]
&& accCo != null
orderby accCom.Code
select new Combined { AccControls = accCo, AccCompoanies = accCom };
答案 2 :(得分:0)
也许是Request.QueryString["ControlCode"]
尝试在linq查询之外声明它,如下所示:
var code = Request.QueryString["ControlCode"];
var q = from accCo in db.AccControls
join accCom in db.AccCompanies
on new { accCo.ControlCode } equals
new { ControlCode = accCom.Code }
where accCo.ControlCode == code //EDIT
orderby accCom.Code
select new Combined{ AccControls = accCo, AccCompoanies = accCom };