Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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 model class
C#
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.
Posted
Updated 11-Mar-14 6:36am
v2

1 solution

I think the problem lies in this condition :
where r.role == r.ID &&
Not sure if role and ID are related
 
Share this answer
 
Comments
miss786 11-Mar-14 13:38pm    
Many thanks. That solved it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900