Skip to main content
easier to understand
Source Link
wingyip
  • 3.5k
  • 4
  • 36
  • 57

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 AspNetUsersUsers u inner join ClientUserClientUsers 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?

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 AspNetUsers u inner join ClientUser 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?

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?

Source Link
wingyip
  • 3.5k
  • 4
  • 36
  • 57

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.

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 AspNetUsers u inner join ClientUser 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?