none
Find records which id is contained in list of int RRS feed

  • Question

  • Hello,

    I have the following list:

    IList<Int32> roles = { 1, 2, 3 }

    I need to get all users where in user.Roles there is at least one Role which is id 1, 2 OR 3.
    var a = users.Where(u => u.Roles. ??? );

    How can I do this?

    Thanks,
    Miguel
    Friday, May 21, 2010 4:54 PM

Answers

All replies

  • List<int> roles = new List<int>() {1,2,3};

    var a = users.Where( u=> roles.Contain( u.Role ) ); 

    Friday, May 21, 2010 4:57 PM
  • Sorry ... Maybe I didn't explain it well:

      var a = users.Where(u => u.Roles .... Each one ID contains 1);

    Basically each User has many Roles. Each Role has an ID.

    I need to get all users which Roles do not contain any Role with ID = 1.

    Basically I need to get all users that are not Admin.

    Well, solving this would be enough ...

     

    Friday, May 21, 2010 5:23 PM
  • var a = users.Where( u => u.Roles.Any( r => r.RoleID != 1 ) )
    • Marked as answer by MDMoura Friday, May 21, 2010 5:36 PM
    Friday, May 21, 2010 5:28 PM
  • I had tried something similar before which is closer to what I want:

    users.Where(u => !u.Roles.Any(r => r.Id == 1))

    The problem was that u.Roles was null and I thought the problem was in my Linq Query.

    Just solved it.

    Thanks,

    Miguel

    Friday, May 21, 2010 5:36 PM