-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect SQL when using Linq 'Any' #5084
Comments
Can you share your model & dbcontext classes? |
It is for a customer who would not be happy to share the model with public. Can I do this privately? |
No problem. Which version of EF are you using? |
{ "dependencies": { "commands": { "frameworks": { |
I have a 'using AutoMapper.QueryableExtensions'. I'll check that this isn't causing the issue. |
This looks similar to #4877 which is fixed in latest codebase (but causes issue in rc1 release). I will test if above query works in latest code or not. |
OK I have tested and it has nothing to do with AutoMapper.QueryableExtensions. |
Repro code: public class Program
{
public static void Main(string[] args)
{
using (var _context = new BloggingContext())
{
_context.Database.EnsureDeleted();
_context.Database.EnsureCreated();
var workProgramId = 1;
var query = _context.Sites
.Where(b => b.WorkTasks.Any(c => c.WorkProgramId == workProgramId))
.Where(c => c.IsDeleted == false)
.Count();
}
}
}
public class Site
{
public int Id { get; set; }
public bool IsDeleted { get; set; }
public ICollection<WorkTask> WorkTasks { get; set; }
}
public class WorkTask
{
public int Id { get; set; }
public int SiteId { get; set; }
public int WorkProgramId { get; set; }
}
public class BloggingContext : DbContext
{
public DbSet<Site> Sites { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Database=test;Integrated Security=True;Connect Timeout=30");
}
} Generated query in latest code exec sp_executesql N'SELECT COUNT(*)
FROM [Sites] AS [b]
WHERE EXISTS (
SELECT 1
FROM [WorkTask] AS [c]
WHERE ([c].[WorkProgramId] = @__workProgramId_0) AND ([b].[Id] = [c].[SiteId])) AND ([b].[IsDeleted] = 0)',N'@__workProgramId_0 int',@__workProgramId_0=1 The issue is fixed in latest code. Feel free to re-open, if you still see issue happening after upgrading to RC2 (after it releases). |
Excellent thank you. |
_context.Sites
.Where(b => b.WorkTasks.Any(c => c.WorkProgramId == workProgramId))
.Where(c => c.IsDeleted == false)
.Count()
Generates the following SQL
When it should have the '=1' after the WHERE (..)? i.e.
The text was updated successfully, but these errors were encountered: