0

我需要从表中的主键与当前 UserId 匹配的表中获取 id 值。我正在使用 entityframework/mvc 登录表(UserProfile、UsersInRoles 和 Roles),但我有另一个名为 CompanyUser 的表,它与 UserProfile 没有关系。由于项目的构建方式,我无法在它们之间建立关系。

在我的 CompanyUser 表中,我有列 UserID 和 CompanyID,UserID 与表 UserProfile 中的 UserID 完全相同。所以我需要做的但不知道我可以使用.equals 或.where 的其他方法是获取特定的CompanyID,其中同一个表中的UserID 与websecurity.currentUserID 匹配。

4

1 回答 1

1

这并不完全清楚,但我怀疑你只是想要:

var currentUserId = websecurity.CurrentUserID;
var companyId = db.Companies
                  .Where(x => x.UserID == currentUserId)
                  .Select(x => x.CompanyID)
                  .Single();

更改Single为,First或者FirstOrDefault如果您不知道会有匹配的记录,或者可能有多个。

于 2013-05-29T07:24:46.657 回答