C# Linq选择具有匹配子项的父/父记录
使用ef core并尝试根据子项中的条件选择父项 如果我在sql中这样做,我会C# Linq选择具有匹配子项的父/父记录,c#,linq,linq-to-entities,C#,Linq,Linq To Entities,使用ef core并尝试根据子项中的条件选择父项 如果我在sql中这样做,我会 declare @UserName nvarchar(200) set @UserName = 'al65272' declare @ClientId int set @ClientId = 32 select u.* from Users u inner join ClientUsers cu on u.Id = cu.UserId where NormalizedUserName = upper(@
declare @UserName nvarchar(200)
set @UserName = 'al65272'
declare @ClientId int
set @ClientId = 32
select u.*
from Users u
inner join ClientUsers cu on u.Id = cu.UserId
where NormalizedUserName = upper(@UserName) and cu.ClientId=@ClientId
我想我可以这样做:
var userByUserName = _applicationDbContext.Users.FirstOrDefault(x =>
x.NormalizedUserName == userName.ToUpper() &&
x.ClientUsers.Contains(new ClientUser {ClientId = client.Id, User = x}));
但显然是错误的,因为没有返回任何内容
有人能给我指出正确的方向吗?我想这对你来说很有用:
var userByUserName = _applicationDbContext.Users.FirstOrDefault(x =>
x.NormalizedUserName == userName.ToUpper() &&
x.ClientUsers.Any(c => c.ClientId == client.Id));
在提供的SQL中,您寻址两个表AspNetUsers和ClientUser,但在LINQ中,您寻址用户。你能用正确的数据编辑你的帖子吗?对不起,谢谢。现在更正