获取用户的角色linq查询 [英] get user's role linq query

查看:90
本文介绍了获取用户的角色linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个linq查询,它可以将用户名与其角色匹配。我已经测试了以下代码,它使用test api控制器没有给我任何响应(没有找到任何角色)。



I am trying to build a linq query, which can match up username to its role. I have tested the following code and its giving me no response (no role found) using test api controller.

public  string[] GetRolesForUser(string userName)
{

   // User user = db.api_login.FirstOrDefault(u => u.username.Equals(userName, StringComparison.CurrentCultureIgnoreCase)); //|| u.Email.Equals(username, StringComparison.CurrentCultureIgnoreCase));

    var roles = from r in db.api_login
                where r.role == r.ID &&
                r.username == userName
                    select r.role;
        if (roles != null)
            return roles.ToArray();
        else
            return new string[] { }; ;
}





api_login模特班



api_login model class

public partial class api_login
    {
        public string ID { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public string description { get; set; }
        public string role { get; set; }
    }





我错过了什么吗?请帮忙。非常感谢。



Am I missing something? Please help. Many thanks.

推荐答案

我认为问题出在这种情况:

其中r.role == r .ID&&

不确定角色和ID是否相关
I think the problem lies in this condition :
where r.role == r.ID &&
Not sure if role and ID are related


这篇关于获取用户的角色linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆