locked
How to specify multiple data with EF contains function RRS feed

  • Question

  • User828542847 posted

    suppose i am checking user has any one of role or not. suppose all roles are stored in variable separated in comma

    var roles ="'HR','Admin','Account'" etc

    now how could i detect that a specific user has any of the above role by EF query ?

    i want to use contains function where like to provide all roles name separated by comma.

    suppose tell me how could i compose this sql by EF

    select count(*) from UserRoles where userid=1 and rolename in (roles)

    i know the above sql is not compile....it is just a pesudo sample which help some one to understand what kind of query i want to compose by EF.

    i got one closer EF query but not sure does it work or not.

    string userId = User.Identity.GetUserId();
    ApplicationDbContext db = new ApplicationDbContext();
    var role = (from r in db.Roles where r.Name.Contains("Admin") select r).FirstOrDefault();
    var users = db.Users.Where(x => x.Roles.Select(y => y.RoleId).Contains(role.Id)).ToList();
    if (users.Find(x => x.Id == userId) != null)
    {
    // User is in the Admin Role
    }
    else
    {
    
    //User is not in the Admin Role
    }

    guide me how could i achieve this.

    Monday, July 17, 2017 11:45 AM

Answers

  • User1068175894 posted

    var roleList = roles.Split(',');

    var users = from user in context.Users where roleList.Contains(user.role) select user;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 17, 2017 12:08 PM
  • User1120430333 posted

    EF doesn't have a Contains() function it's a Linq function.

    I suggest that you figure out how to use a List<string> two  Lists one contains the Roles and the other List that contains Roles for a given user.

    https://stackoverflow.com/questions/9032655/check-if-a-string-within-a-list-contains-a-specific-string-with-linq

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 17, 2017 12:15 PM
  • User828542847 posted

    @DA thanks this will works

    if( myList.Any( s => s.Contains(stringToCheck))){
      //do your stuff here
    }
    
    OR
    
    bool contains = myList.Any(s => s.Contains(pattern));

    Got the right code

    var roles = "'HR','Admin','Account'";
    string[] arrayRoles = roles.Split(',');
    
    
    //get count
    
     var count = db.UserRoles.Where(t => t.userid == 1 && arrayRoles.Contains(t.rolename)).Count();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 17, 2017 12:29 PM

All replies

  • User1068175894 posted

    var roleList = roles.Split(',');

    var users = from user in context.Users where roleList.Contains(user.role) select user;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 17, 2017 12:08 PM
  • User1120430333 posted

    EF doesn't have a Contains() function it's a Linq function.

    I suggest that you figure out how to use a List<string> two  Lists one contains the Roles and the other List that contains Roles for a given user.

    https://stackoverflow.com/questions/9032655/check-if-a-string-within-a-list-contains-a-specific-string-with-linq

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 17, 2017 12:15 PM
  • User828542847 posted

    @DA thanks this will works

    if( myList.Any( s => s.Contains(stringToCheck))){
      //do your stuff here
    }
    
    OR
    
    bool contains = myList.Any(s => s.Contains(pattern));

    Got the right code

    var roles = "'HR','Admin','Account'";
    string[] arrayRoles = roles.Split(',');
    
    
    //get count
    
     var count = db.UserRoles.Where(t => t.userid == 1 && arrayRoles.Contains(t.rolename)).Count();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 17, 2017 12:29 PM
  • User828542847 posted

    Custom WhereIn Extension

    https://stackoverflow.com/questions/15829031/linq-to-entities-compare-value-against-listint
    https://stackoverflow.com/questions/374267/contains-workaround-using-linq-to-entities?rq=1

    Monday, July 17, 2017 2:36 PM