Linq查询无法使用其他查询

时间:2016-06-22 22:32:27

标签: c# asp.net-mvc linq

我是linq的新手,我想使用一个显示employees表中主管名称的查询。表结构是:

Users(
Id int, 
usrFirstName nvarchar(20),
usrLastName nvarchar(20),
usrSupervisor int
)

我的linq查询:

        User userData = (User)Session["user"];
        var db = new scaleDBEntities();
        return this.Json((from userObj in db.Users
                          where userObj.Id.Equals(from idObj in db.Users
                                                  where idObj.Id.Equals(userData.Id)
                                                  select idObj.usrSupervisor)
                          select new
                          {
                              supervisorId = userObj.Id,
                              supervisorfName = userObj.usrFirstName,
                              supervisorlName = userObj.usrLastName,                                  
                          })
                          , JsonRequestBehavior.AllowGet
                        );

如您所见,当前用户ID存储在会话变量中,并在查询中用于查找主管名称。

好吧,这个查询不起作用...更具体地说,问题在于内部查询,因为当我用一个数字替换查询以表示随机ID时,它可以工作

        User userData = (User)Session["user"];
        var db = new scaleDBEntities();
        return this.Json((from userObj in db.Users
                          where userObj.Id.Equals(1)
                          select new
                          {
                              supervisorId = userObj.Id,
                              supervisorfName = userObj.usrFirstName,
                              supervisorlName = userObj.usrLastName,                                  
                          })
                          , JsonRequestBehavior.AllowGet
                        );

2 个答案:

答案 0 :(得分:1)

很抱歉,我通常不会使用查询语法,因此我使用方法语法将代码放下。 我已将流程分为几个小步骤,以便更容易找到问题。 您可以尝试下面的代码,看看是否有任何错误吗?

User userData = (User)Session["user"];
var db = new scaleDBEntities();

// grab user object
var usr = db.Users.FirstOrDefault(u => u.Id == userData.Id);
if(usr == null) throw new Exception("Can't find user!");

// grab supervisor object
var superid = usr.usrSupervisor;
var supervisor = db.Users.FirstOrDefault(s => s.Id == superid);
if(supervisor == null) throw new Exception("Can't find supervisor");

return this.Json( new { supervisorId = supervisor.Id, 
                        supervisorfName = supervisor.usrFirstName, 
                        supervisorlName = supervisor.usrLastName,}
                  , JSonRequestBehavior.AllowGet);

修改

使用查询来将管理员ID作为整数获取,

var superid = db.Users.Where(u => u.Id = userData.Id)
                      .Select(u => u.usrSupervisor)
                      .FirstOrDefault();

答案 1 :(得分:0)

如果Users有自己的FK,你应该有一个导航属性(可能称为User)给主管。

User userData = (User)Session["user"];
    var db = new scaleDBEntities();
    return this.Json((from userObj in db.Users
                      where userObj.Id.Equals(userData.Id)
                      select new
                      {
                          supervisorId = userObj.User.Id,
                          supervisorfName = userObj.User.usrFirstName,
                          supervisorlName = userObj.User.usrLastName,                                  
                      })
                      , JsonRequestBehavior.AllowGet
                    );