question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query: Using Or instead of ElseOr, etc. can result in unexpected SQL

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
smitpatelcommented, Feb 14, 2017

Yes.

1reaction
smitpatelcommented, Feb 14, 2017

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL query using EXIST operator - unexpected records in ...
I figured out what was going on by looking at the query plan. Essentially, the subquery returns 1 row which makes exists true,...
Read more >
Unexpected SQL Warning - OutSystems 11 Documentation
You have an SQL query where the specified Output Structure does not match the attributes being selected. This can happen because the number...
Read more >
SQL Query Returned Unexpected Results
Use this guide to troubleshoot queries that don't return the information you want. Note: If you don't know the SQL programming language, ...
Read more >
syntax error line 6 at position 35 unexpected 'ON'.
In vim, I did ":%s/[^\x00-\x7F]/ /g", and after that the query worked - this just replaces any non-ASCII characters with a space.
Read more >
Common SQL syntax errors and how to resolve them
In this article, we are going to describe some of the most common SQL syntax errors, and explains how you can resolve these...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found