1

Using ef core and trying to select parent items according to criteria in the child item.

If I was doing this in sql I would

 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?

2
  • In the provided SQL you address two tables AspNetUsers and ClientUser but in the LINQ you address Users. Could you edit your post with the proper data. Commented Jan 28, 2020 at 16:04
  • Sorry thanks. Corrected now Commented Jan 28, 2020 at 16:10

1 Answer 1

2

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)); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.