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: Using Or instead of ElseOr, etc. can result in unexpected SQL #7271

Closed
datasecurity opened this issue Dec 19, 2016 · 6 comments
Closed
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

@datasecurity
Copy link

datasecurity commented Dec 19, 2016

When I use two conditions joined by an OR, the result is not correct for SQL Server.

How can I fix it?

This is my LINQ code and result in SQL (that reflection created for me):

query.Where(p => ((p.Code == "100000") Or p.Code.EndsWith("200")));
query.Where(p => (p.year == "2015"))}

I added this where clause at runtime, now I add another extension method and it's not working:

query.sum(p => p.value)

Exception:

An exception of type 'System.Data.SqlClient.SqlException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code

Additional information: An expression of non-boolean type specified in a context where a condition is expected, near 'AND'.

SQL translated:

SELECT SUM([e].[Value])
FROM [acc].[Data161] AS [e]
WHERE (CASE
          WHEN RIGHT([e].[Code], LEN(N'201')) = N'201'
             THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
       END | 
       CASE
          WHEN RIGHT([e].[Code], LEN(N'199')) = N'199'
             THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
       END) 
  AND ([e].[SetadCode] = N'161')

The correct SQL should have = 1 before the AND.

But without sum its works fine and add a = 1 to SQL command

tuespetre added a commit to tuespetre/EntityFramework that referenced this issue Dec 30, 2016
@tuespetre
Copy link
Contributor

This turned out not be because of the Sum but because of the interaction between BinaryExpressions and ConditionalExpressions.

@divega divega added this to the 2.0.0 milestone Dec 30, 2016
@divega
Copy link
Contributor

divega commented Dec 30, 2016

Assigning issue and PR to @smitpatel.

smitpatel added a commit that referenced this issue Feb 4, 2017
smitpatel added a commit that referenced this issue Feb 4, 2017
smitpatel added a commit that referenced this issue Feb 7, 2017
smitpatel added a commit that referenced this issue Feb 7, 2017
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 7, 2017
@liamdawson
Copy link

Does someone have a workaround for this issue?

@smitpatel
Copy link
Contributor

Use OrElse instead of Or (Or is bitwise operator, OrElse is logical operator)

@liamdawson
Copy link

Would AndAlso also be the appropriate change for And expressions?

@smitpatel
Copy link
Contributor

Yes.

@ajcvickers ajcvickers changed the title Conditional sum using linq for Or condition Query: Using Or instead of ElseOr, etc. can result in unexpected SQL 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

6 participants