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

Calling Contains on an empty array returns int, not bool #12690

Closed
tomp31 opened this issue Jul 17, 2018 · 3 comments
Closed

Calling Contains on an empty array returns int, not bool #12690

tomp31 opened this issue Jul 17, 2018 · 3 comments

Comments

@tomp31
Copy link

tomp31 commented Jul 17, 2018

Executing the following LINQ query does not work in EF Core 2.1.1 (where Countries is an arbitrary DbSet and CountryId is an arbitrary int property):

var ids = new List<int>();
await _context.Countries.Select(e => new GetCountryDto
{
    BoolVariable = ids.Contains(e.CountryId)
}).ToListAsync(cancellationToken);

Looking at the EF logs, I see the starts of an attempt to translate the query:

Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
Optimized query model: 
'from Country e in DbSet<Country>
select new GetCountryDto{ BoolVariable = 
        (from int <generated>_1 in __ids_0
        select [<generated>_1]).Contains([e].CountryId) }
'

but the command eventually run is

Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
SELECT 0 AS [BoolVariable]
FROM [General].[Countries] AS [e]

Because the 0 returned is an int and not a bool/bit, I get System.InvalidOperationException: An exception occurred while reading a database value. The expected type was 'System.Boolean' but the actual value was of type 'System.Int32'.

However, if I change the array on which Contains is called to not be empty:
var ids = new List<int> {1};
then the SQL generated is correct and includes CASTs:

Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
SELECT CASE
    WHEN [e].[CountryId] IN (1)
    THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END AS [BoolVariable]
FROM [General].[Countries] AS [e]

EF Core version: 2.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.5

@maumar
Copy link
Contributor

maumar commented Jul 17, 2018

possible dupe of #12280

@divega divega added this to the 2.2.0 milestone Jul 17, 2018
@maumar
Copy link
Contributor

maumar commented Jul 17, 2018

I verified this is a dupe of #12280 - this issue will be fixed in the next patch (2.1.3)

@tomp31
Copy link
Author

tomp31 commented Jul 18, 2018

Thank you and sorry for not finding the duplicate issue.

@ajcvickers ajcvickers removed this from the 2.2.0 milestone Jul 25, 2018
@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
Projects
None yet
Development

No branches or pull requests

4 participants