You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
Executing the following LINQ query does not work in EF Core 2.1.1 (where
Countries
is an arbitrary DbSet andCountryId
is an arbitrary int property):Looking at the EF logs, I see the starts of an attempt to translate the query:
but the command eventually run is
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:
EF Core version: 2.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.5
The text was updated successfully, but these errors were encountered: