最近使用ASP.NET identity 做用户查询碰到个很棘手问题,求各位大神帮我看看

使用identity 系统会默认生成 User、Role 和UserRole三张表,但是我在User表中加了CompanyId字段。我想查询当前 CompanyId是001的Role 是 01(admin)的所有用户。
如果是Sql直接就:select * from user,userrole where user.userid=userrole.userid and user.companyid="001" and userrole.roleid="01" 就好了。

但是由于小弟刚刚接触MVC比较愚钝,用linq不管怎么试都不行。我把我试的代码贴在下面:

尝试一: 
AppRole role = RoleManager.FindById("R02");
var users = UserManager.Users.Where(u => u.Roles= role && u.Company="001" );

尝试二:
var users= UserManager.Users.Where(u => u.Roles.SingleOrDefault().RoleId = "01" && u.Company="001" )



AppRole 类:
    public class AppRole : IdentityRole
    {

        public AppRole() : base() { }

        public AppRole(string name) : base(name) { }
    }
AppUser 类:
    public class AppUser : IdentityUser
    {
        // 这里将放置附加属性
        public AppUser() : base() { }
        [Required]
        public int CompanyId { get; set; }
        public virtual Company Company { get; set; }
    }
AppUserRole类 :
    public class AppUserRole:IdentityUserRole
    {
        public AppUserRole() : base() { }
    }

HOYA
浏览 2600回答 3
3回答

习惯受伤

u => u.Roles= role && u.Company="001" 这种逻辑判断用==而不是=,也就是写成这样: u => u.Roles== role && u.Company=="001"
打开App,查看更多内容
随时随地看视频慕课网APP