I am trying to get a list from Locations table and exclude the Locations already assigned to a user. I am trying to using Linq FromSQL. If I hard code the user.Id parameter the query works, when I use the user.Id variable it does not work. Query returns all records. When I run the debugger, the user.Id variable is correct. Here is my code...
ApplicationUser user = await _userManager.FindByNameAsync(Id); var unassignedLocations = await _context.Location .FromSql($"SELECT * FROM Locations WHERE Id NOT IN (SELECT LocationId FROM UserLocations WHERE UserID='{user.Id}')") .OrderBy(l => l.Name) .ToListAsync(); Locations = new SelectList(unassignedLocations, "Id", "Name");