Hello,
Below query works fine in SQL
SELECT A.Email
FROM [model].[dbo].[AspNetUsers] A, [model].[dbo].[SolUserStudent] C
where A.Id = C.SolUserId and
A.Id NOT IN (SELECT [SolUserId] FROM [model].[dbo].[SolUserStudent] where SolStudentId = 'A50F9946-01DE-434A-8791-27B179ED153D')
Can anyone help in how to convert the same in LINQ. I have got so far but struggling how to put in the NOT IN
var users = await (
from user in _context.SolUser
join sol in _context.SolUserStudent
on user.Id equals sol.SolUserId
where user.Id ??? (from o in _context.SolUserStudent where o.SolStudentId == request.Id select o.SolUserId)
select user).ToListAsync();

Jignesh KumarRank #91 · 22.9K reputationPosted May 17, 2023, 11:35 PM EDT
Hello Dave Bell,
please use this one,
var result = await (from a in context.AspNetUsers join c in context.SolUserStudent on a.Id equals c.SolUserId where a.Id != await context.SolUserStudent .Where(s => s.SolStudentId == "A50F9946-01DE-434A-8791-27B179ED153D") .Select(s => s.SolUserId) .FirstOrDefaultAsync() select a.Email) .ToListAsync();Chintan SukhadiyaRank #9,451 · 206 reputationPosted May 18, 2023, 3:28 AM EDT
var result = await _context.AspNetUsers .Join(_context.SolUserStudent, a => a.Id, s => s.SolUserId, (a, s) => new {a, s}) .Where(_as => !_context.SolUserStudent.Where(su => su.SolStudentId = "A50F9946-01DE-434A-8791-27B179ED153D").Select(su => su.SolStudentId).Contains(_as.s.SolStudentId)) .Select(_as => _as.a.Email).ToListAsync();Naimish MakwanaRank #324 · 7.7K reputationPosted May 18, 2023, 12:30 AM EDT
Try below:
var excludedStudentId = "A50F9946-01DE-434A-8791-27B179ED153D"; var result = from a in dbContext.AspNetUsers join c in dbContext.SolUserStudent on a.Id equals c.SolUserId where a.Id != excludedStudentId && !(from s in dbContext.SolUserStudent where s.SolStudentId == excludedStudentId select s.SolUserId).Contains(a.Id) select a.Email;Thanks
Dave Bell100 reputationPosted May 18, 2023, 8:08 AM EDT
Sorry I missed to mention its N:N relationship. Below worked
var users = await (
from user in _context.SolUser
join sol in _context.SolUserStudent
on user.Id equals sol.SolUserId
where !(from o in _context.SolUserStudent where o.SolStudentId == request.Id select o.SolUserId).Contains(user.Id)
select user).Distinct().ToListAsync();