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

Query: Simplifying Ternary expression in body of a Linq query breaks the query. #6695

Closed
paintballduke22 opened this issue Oct 6, 2016 · 2 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@paintballduke22
Copy link

Steps to reproduce

1: Create a users table that has a UserId int, HomePhone string, and MobilePhone string.
2: Insert a single record (UserId = 1, HomePhone = '1234567890', MobilePhone = '')

The issue

The following query works as intended:

 var results =
                from users in _db.Users                
                where users.UserId == 1
                select new
                {
                    UserID = users.UserId,
                    UserPhone = new
                    {
                        Number = users.HomePhone,
                        IsPrimary = string.IsNullOrEmpty(users.MobilePhone) ? false : true
                    },
                };

But simplifying the ternary operator on IsPrimary breaks the query and returns no results:

 var results =
                from users in _db.Users                
                where users.UserId == 1
                select new
                {
                    UserID = users.UserId,
                    UserPhone = new
                    {
                        Number = users.HomePhone,
                        IsPrimary = !string.IsNullOrEmpty(users.MobilePhone)
                    },
                };

Below are the exception that was thrown and the Stack Trace.

Exception message: [Error] An exception occurred in the database while iterating the results of a query.

Stack Trace:
System.Data.SqlClient.SqlException: An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean openConnection, Boolean closeConnection)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues, Boolean manageConnection)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3`1.MoveNext()
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
ClientConnectionId:392b7cbe-a1b6-4885-96e8-722ed2d776a2
Error Number:4145,State:1,Class:15

Further technical details

EF Core version: 1.0.1
Operating system: Windows 10
Visual Studio version: VS 2015

@paintballduke22
Copy link
Author

The resulting sql that is generated is here as well.

Working query:
SELECT [users].[UserID], [users].[HomePhone], CASE WHEN [users].[MobilePhone] IS NULL OR ([users].[MobilePhone] = '') THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT) END FROM [Users] AS [users] WHERE [users].[UserID] = 1

Non-working query:
SELECT [users].[UserID], [users].[HomePhone], CASE WHEN CASE WHEN [users].[MobilePhone] IS NULL OR ([users].[MobilePhone] = '') THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT) END FROM [Users] AS [users] WHERE [users].[UserID] = 1

@rowanmiller rowanmiller added this to the 1.2.0 milestone Oct 6, 2016
@maumar
Copy link
Contributor

maumar commented Oct 28, 2016

This is now fixed. Currently we generate:

SELECT [users].[UserId], [users].[HomePhone], CASE
    WHEN [users].[MobilePhone] IS NOT NULL AND (([users].[MobilePhone] <> N'') OR [users].[MobilePhone] IS NULL)
    THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END
FROM [Users] AS [users]
WHERE [users].[UserId] = 1

@maumar maumar closed this as completed Oct 28, 2016
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 28, 2016
@ajcvickers ajcvickers changed the title Simplifying Ternary expression in body of a Linq query breaks the query. Query: Simplifying Ternary expression in body of a Linq query breaks the query. May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants