Kirk
Kirk

Reputation: 5077

How to join Enum with LINQ Query

I have user table (Default ApplicationUser Table from IdentityUser by ASP.CORE) and I have added additional field for RoleType. There is also an Enum I added to specify Role Definition.

public enum Roles
{
    Administrator = 1,
    Headquarters = 2,
    Branch = 3,
    Driver = 4,
    Client = 5
}

Now I want to show all the users in a view as a table along with role description.

I am unable to make LINQ query with Enum & User table using LINQ join.

Upvotes: 3

Views: 6726

Answers (4)

Gauravsa
Gauravsa

Reputation: 6524

// This join is performed in memory
var results =
    from e in Enum.GetValues(typeof(Roles)).Cast<Roles>()
    join r in ApplicationUser on e equals r.Roles into rs
    from r in rs.DefaultIfEmpty()
    select new { Roles = e, Count = r?.Count ?? 0};

Upvotes: 1

DarkSquirrel42
DarkSquirrel42

Reputation: 10257

var xx = from u in _context.Users
         .Select(x => new ApplicationUserList 
                { Firstname = x.Firstname, 
                  RoleType = ((Roles)x.RoleId).ToString() 
                }); 

Upvotes: 1

Steve Ford
Steve Ford

Reputation: 7753

To get the list of Roles from the enum use:

var roles = Enum.GetValues(typeof(Roles)).Cast<Roles>()
        .Select(r => new { Value = (int)r, Name = r.ToString() }).ToList();

you can then use this in your Linq query, for example:

var roles = Enum.GetValues(typeof(Roles)).Cast<Roles>()
        .Select(r => new { Value = (int)r, Name = r.ToString() }).ToList();

var users = from u in ApplicationUser
        join r in roles on u.Role equals r.Value
        select new {Name = u.Name, RoleId = u.Role, RoleDescription = r.Name} ;

A simpler way without the Enum.GetValues is:

var users = from u in ApplicationUser
        select new {Name = u.Name, RoleId = u.Role, RoleDescription = (Roles)r.Role.ToString()} ;

Upvotes: 6

Greg Uretzky
Greg Uretzky

Reputation: 62

If I understand your question, you should first convert enum to dictionary an Join between what you need, here is an example:

    static void Main(string[] args)
    {
        ApplicationUser a = new ApplicationUser();
        a.userName = "a";
        a.role = 1;

        ApplicationUser b = new ApplicationUser();
        b.userName = "b";
        b.role = 3;

        List<ApplicationUser> alist=new List<ApplicationUser>();
        alist.Add(a);
        alist.Add(b);

        Dictionary<int, string> DicRoles = new Dictionary<int, string>();
        var vals = Enum.GetValues(typeof(Roles));

        foreach (var val in vals)
        {
            DicRoles.Add((int)val, val.ToString());
        }

        var result = from t in alist
                     join x in DicRoles on t.role equals x.Key
                     select new {t.userName,x.Value };


    }

    public enum Roles:int
    {
        Administrator = 1,
        Headquarters = 2,
        Branch = 3,
        Driver = 4,
        Client = 5
    }
}

public class ApplicationUser
{
    public string userName { get; set; }
    public int role { get; set; }
}

Upvotes: 0

Related Questions