简体   繁体   English

Linq 选择具有匹配子项的父/父记录

[英]Linq select parent/parents record with matching child items

Using ef core and trying to select parent items according to criteria in the child item.使用 ef core 并尝试根据子项中的条件选择父项。

If I was doing this in sql I would如果我在 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(@UserName) and cu.ClientId=@ClientId

I thought I could do something like this:我以为我可以做这样的事情:

var userByUserName = _applicationDbContext.Users.FirstOrDefault(x =>
                x.NormalizedUserName == userName.ToUpper() &&
                x.ClientUsers.Contains(new ClientUser {ClientId = client.Id, User = x}));

But obviously wrong as does not return anything.但显然是错误的,因为没有返回任何东西。

Can anyone point me in the right direction?任何人都可以指出我正确的方向吗?

I think this would work for you :我认为这对你有用:

var userByUserName = _applicationDbContext.Users.FirstOrDefault(x =>
                x.NormalizedUserName == userName.ToUpper() &&
                x.ClientUsers.Any(c => c.ClientId == client.Id));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM