Skip to content
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

Closed
agileorange opened this issue Apr 15, 2016 · 9 comments
Closed

Incorrect SQL when using Linq 'Any' #5084

agileorange opened this issue Apr 15, 2016 · 9 comments

Comments

@agileorange
Copy link

agileorange commented Apr 15, 2016

_context.Sites
.Where(b => b.WorkTasks.Any(c => c.WorkProgramId == workProgramId))
.Where(c => c.IsDeleted == false)
.Count()

Generates the following SQL

SELECT COUNT(*)
FROM [Site] AS [b]
INNER JOIN [Location] AS [b.Location] ON [b].[LocationId] = [b.Location].[Id]
INNER JOIN [SiteClient] AS [b.SiteClient] ON [b].[SiteClientId] = [b.SiteClient].[Id]
INNER JOIN [SiteCategory] AS [b.SiteCategory] ON [b].[SiteCategoryId] = [b.SiteCategory].[Id]
WHERE (
    SELECT CASE
        WHEN EXISTS (
            SELECT 1
            FROM [WorkTask] AS [c]
            WHERE ([c].[WorkProgramId] = @__workProgramId_0) AND ([b].[Id] = [c].[SiteId]))
        THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
    END
) AND ([b].[IsDeleted] = 0)

When it should have the '=1' after the WHERE (..)? i.e.

SELECT COUNT(*)
FROM [Site] AS [b]
INNER JOIN [Location] AS [b.Location] ON [b].[LocationId] = [b.Location].[Id]
INNER JOIN [SiteClient] AS [b.SiteClient] ON [b].[SiteClientId] = [b.SiteClient].[Id]
INNER JOIN [SiteCategory] AS [b.SiteCategory] ON [b].[SiteCategoryId] = [b.SiteCategory].[Id]
WHERE (
    SELECT CASE
        WHEN EXISTS (
            SELECT 1
            FROM [WorkTask] AS [c]
            WHERE ([c].[WorkProgramId] = @__workProgramId_0) AND ([b].[Id] = [c].[SiteId]))
        THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
    END
) **= 1** AND ([b].[IsDeleted] = 0)
@smitpatel
Copy link
Member

Can you share your model & dbcontext classes?

@agileorange
Copy link
Author

It is for a customer who would not be happy to share the model with public. Can I do this privately?

@smitpatel
Copy link
Member

No problem. Which version of EF are you using?

@agileorange
Copy link
Author

{
"version": "1.0.0-*",
"description": "Apollo1.Web.Data Class Library",
"authors": [ "mark.chidlow" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",

"dependencies": {
"Apollo1.Web.Domain": "1.0.0-*",
"EntityFramework.Commands": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final"
},

"commands": {
"ef": "EntityFramework.Commands"
},

"frameworks": {
"dnx451": { },
"dnxcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23409",
"System.Collections": "4.0.11-beta-23409",
"System.Linq": "4.0.1-beta-23409",
"System.Runtime": "4.0.21-beta-23409",
"System.Threading": "4.0.11-beta-23409"
}
}
}
}

@agileorange
Copy link
Author

I have a 'using AutoMapper.QueryableExtensions'. I'll check that this isn't causing the issue.

@smitpatel
Copy link
Member

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.

@agileorange
Copy link
Author

OK I have tested and it has nothing to do with AutoMapper.QueryableExtensions.

@smitpatel
Copy link
Member

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).
As a work-around in RC1 you can convert Any(...) to Count(...) > 0

@agileorange
Copy link
Author

Excellent thank you.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants