1

I'm a bit new to Linq and hoping someone could help me on something I'm currently strugling.

I currently have 2 list

public IQueryable<TRole> Roles
{
   get
   {
      var roles = new List<TRole>();
      roles.Add(CreateIdentityRole("Chairman", "Voorzitter"));
      roles.Add(CreateIdentityRole("Treasurer", "Penningmeester"));
      roles.Add(CreateIdentityRole("Ranking Responsible", "Rankingverantwoordelijke"));
                roles.Add(CreateIdentityRole("Competition Responsible", "Competitieverantwoordelijke"));
      roles.Add(CreateIdentityRole("Webmaster", "Webbeheerder"));
      roles.Add(CreateIdentityRole("Secretary", "Secretaris"));
      foreach(TRole role in roles)
      {
         if (!_roles.Contains(role))
            _roles.Add(role);
      }

      return _roles.AsQueryable<TRole>();
   }
}

private TRole CreateIdentityRole(string id, string name)
{
   TRole role = (TRole)Activator.CreateInstance(typeof(TRole));
   role.Id = id;
   role.Name = name;
   return role;
}

public void InitializePrivacy(IdentityUser user)
{
   var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
   var roleManager = new RoleStore<IdentityRole>();
   var roles = roleManager.Roles;
   List<string> userRoles = manager.GetRoles(user.Id);
}

right now I want to use Linq to get all roles which are not found in userRoles. Because the roles is a Queryable I do not really now how to achieve such result.

2
  • Queryable is a static class which provides methods for IQueryable implementations. You can't create an instance of it. Commented Apr 6, 2017 at 10:53
  • the Querable is achieved in another function. This is made to have an example of what I need to achieve. I adapted my script to show you where its created. Commented Apr 6, 2017 at 10:57

3 Answers 3

2

You can use IEnumerable interface. If your method return Querable result it will work. Try this:

IEnumerable<string> roles = roleManager.Roles;
List<string> userRoles = GetRoles(user.Id);

var result = roles.Except(userRoles);
Sign up to request clarification or add additional context in comments.

Comments

2

A simple .Except() should probably be enough:

using System.Linq;
//...
var roles = roleManager.Roles.ToList();
List<string> userRoles = manager.GetRoles(user.Id);
var ununsedRoles = roles.Execpt(userRoles);

This will give you all items roles, except those in userRoles.

MSDN: https://msdn.microsoft.com/en-us/library/bb300779(v=vs.110).aspx

Comments

1

To get a collection of Roles that are not found in userRoles:

var rolesNotFound = roleManager.Roles
                      .Where(r => !userRoles.Select(u => u.Id).Contains(r.Id))
                      .ToList();

1 Comment

I was hoping to see someone doing the same thing in a more complicated way to get more understanding. thx. I appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.