How to asynchronously wait join operation and use it second join in c#?

asp.net-core c# entity-framework entity-framework-core linq

Question

I have method which I am using 2 join operations. I need to achieve first result and then use it in second join. I used both sync and async approach but as a result I am getting back empty array and warning like this

warn: Microsoft.EntityFrameworkCore.Query[20500]
  The LINQ expression 'where ([org].Name == {from Organization org in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[LibvirtManagement.Models.Organization]) join User user in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[User]) on [org].Id equals [user].OrganizationId where ([user].Username == __username_0) select new <>f__AnonymousType10`1(org = [org].Name)}.ToString())' could not be translated and will be evaluated locally.

My method looks like sync version:

[HttpGet("orgprojectmod")]
public  IActionResult GetOrganizationUsersForModerator()
{
    string userRole = "moderator";
    if (userRole != User.FindFirst(ClaimTypes.Role).Value)
        return BadRequest("You need to be in moderator mode to access this request");

    var username = User.FindFirst(ClaimTypes.Name).Value;

    var test = from org in _context.Organizations
        join user in _context.Users on org.Id equals user.OrganizationId
        where user.Username == username
        select new
        {
            org = org.Name
        };

    var test2 = from org in _context.Organizations
        join user in _context.Users on org.Id equals user.OrganizationId
        where org.Name == test.ToString()
        select new
        {
            usernames = user.Username,
            roles = user.Role,
            org = org.Name
        };

    return Ok(test2);
}

Async version:

        [HttpGet("orgprojectmod")]
        public async  Task<IActionResult> GetOrganizationUsersForModerator()
        {
            string userRole = "moderator";
            if (userRole != User.FindFirst(ClaimTypes.Role).Value)
                return BadRequest("You need to be in moderator mode to access this 
               request");

            var username = User.FindFirst(ClaimTypes.Name).Value;

            var test = await _context.Organizations
                .Join(_context.Users, org => org.Id, user => user.OrganizationId, (org, 
               user) => new {org, user})
                .Where(@t => @t.user.Username == username)
                .Select(@t => new {org = @t.org.Name}).ToListAsync();

            var test2 = await _context.Organizations
                .Join(_context.Users, org => org.Id, user => user.OrganizationId, (org, 
                  user) => new {org, user})
                .Where(@t => @t.org.Name == test.ToString())
                .Select(@t => new {usernames = @t.user.Username, roles = @t.user.Role, org = 
              @t.org.Name}).ToListAsync();

            return Ok(test2);
        }

First of all I need to get OrganizationName and then list all UserName, UserRole and OrganizationName according to this OrganizationName.

1
0
3/24/2020 5:28:11 PM

Accepted Answer

The error is saying you pass wrong param. With your code, you are trying to toString() the whole IEnumerable<> (your "test" variable)

.Where(@t => @t.org.Name == test.ToString())

Change it to

var selectedOrg = test.FirstOrDefault().org;
.....
.Where(@t => @t.org.Name == selectedOrg)
1
3/24/2020 5:32:02 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