[英]How to asynchronously wait join operation and use it second join in c#?
I have method which I am using 2 join operations.我有使用 2 个连接操作的方法。 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.首先,我需要获取 OrganizationName,然后根据这个 OrganizationName 列出所有 UserName、UserRole 和 OrganizationName。
The error is saying you pass wrong param.错误是说您传递了错误的参数。 With your code, you are trying to toString() the whole IEnumerable<> (your "test" variable)使用您的代码,您正在尝试 toString() 整个 IEnumerable<> (您的“测试”变量)
.Where(@t => @t.org.Name == test.ToString())
Change it to将其更改为
var selectedOrg = test.FirstOrDefault().org;
.....
.Where(@t => @t.org.Name == selectedOrg)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.