Join not working as expected (Entity Framework)

c# entity-framework-core identity linq

Question

I can't for the life of me figure out how to join these two tables on UserName using entity framework.

I tried both the statement and the method and neither worked.

The tables definitely have the same user in them

var employees = _context.Employees.Include(e => e.Loc);

//Only show employees with a user role of manager
var managerUsers = await _userManager.GetUsersInRoleAsync("Manager");

var match = (from e in employees
                     join m in managerUsers on e.UserName equals m.UserName
                     select new { Employee = e }).ToList();

So, short code breakdown I get a list of all employees from the database context. I look in user roles to find a list of users with the Manager role. Employee also has a UserName field, and I tried to join them using the UserName field. There is one manager currently returning correctly in both tables with a matching username, yet after this code, match has 0 results.

I also tried it like this:

employees.Join(managerUsers,
               e => e.UserName,
               m => m.UserName,
               (e,m) => new { e }).ToList();   

But that also doesn't return any records. What am I doing wrong?

1
0
1/14/2019 11:37:54 AM

Popular Answer

Figured out a solution myself

        var managerEmployees = new List<Employee>();

        for(int a = 0; a< selectManagersList.Count(); a++)
        {
            var found = await _context.Employees.FirstOrDefaultAsync(u=> u.UserName == managerUsers.ElementAt(a).UserName);
            if (found!=null)
            {
                managerEmployees.Add(found);
            }
        }
0
1/14/2019 2:39:31 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow