2

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

但我收到上述错误

4

3 回答 3

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 };
于 2013-02-07T13:57:06.143 回答
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 };
于 2013-02-07T13:54:49.347 回答
0

也许它的Request.QueryString["ControlCode"]

尝试将其贴在 linq qry 之外,如下所示:

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 };
于 2013-02-07T14:00:46.660 回答