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
{{ message }}
Repository navigation
Query: Using Or instead of ElseOr, etc. can result in unexpected SQL #7271
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:
SELECTSUM([e].[Value])
FROM [acc].[Data161] AS [e]
WHERE (CASE
WHEN RIGHT([e].[Code], LEN(N'201')) = N'201'
THEN CAST(1ASBIT) ELSE CAST(0ASBIT)
END |
CASE
WHEN RIGHT([e].[Code], LEN(N'199')) = N'199'
THEN CAST(1ASBIT) ELSE CAST(0ASBIT)
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
changed the title [-]Conditional sum using linq for Or condition[/-][+]Query: Using Or instead of ElseOr, etc. can result in unexpected SQL[/+]on May 9, 2017
Activity
Handle expressions that result in top-level case statements in Search…
tuespetre commentedon Dec 30, 2016
This turned out not be because of the
Sumbut because of the interaction betweenBinaryExpressions andConditionalExpressions.divega commentedon Dec 30, 2016
Assigning issue and PR to @smitpatel.
Query: Refactor SearchConditionTranslatingVisitor
Query: Refactor SearchConditionTranslatingVisitor
Query: Refactor SearchConditionTranslatingVisitor
Query: Refactor SearchConditionTranslatingVisitor
Query: Refactor SearchConditionTranslatingVisitor
liamdawson commentedon Feb 14, 2017
Does someone have a workaround for this issue?
smitpatel commentedon Feb 14, 2017
Use
OrElseinstead ofOr(Or is bitwise operator, OrElse is logical operator)liamdawson commentedon Feb 14, 2017
Would
AndAlsoalso be the appropriate change forAndexpressions?smitpatel commentedon Feb 14, 2017
Yes.
[-]Conditional sum using linq for Or condition[/-][+]Query: Using Or instead of ElseOr, etc. can result in unexpected SQL[/+]