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

Invalid SQL Server query created using WhereIn() and empty arrays #586

Closed
gwheeloc opened this issue Jul 7, 2022 · 4 comments
Closed

Invalid SQL Server query created using WhereIn() and empty arrays #586

gwheeloc opened this issue Jul 7, 2022 · 4 comments

Comments

@gwheeloc
Copy link

gwheeloc commented Jul 7, 2022

Env: SQLServer 2016, C# 10 sqlKata version 2.3.7

I'm developing this query:
IEnumerable<dynamic> results = (IEnumerable<dynamic>)db.Query(dbTable).Select(dbSelectColumns).WhereIn("Customer", criteria.Customers).WhereIn("Territory", criteria.Territories).WhereIn("ProfitCenter", criteria.ProfitCenters).WhereIn("JobNumber", criteria.JobNumbers).WhereIn("ProjectName", criteria.Projects).WhereIn("AuditorId", criteria.Auditors).WhereIn("SubjectId", criteria.Subjects).WhereIn("Status", criteria.Statuses).WhereRaw(ratingCondition).Get<dynamic>();

The properties of the criteria object are of type List<string. If I submit a query with each property have at least one string value in the list, the query execution is fine.

When one of the Lists is empty, the portion of the query representing that List property looks like this: 1 = 0 /* IN [empty list] */
If I change the 0 to a 1 the query works as expected, otherwise no results are returned from the query. Here is a complete example when the customer list has one entry in it and the rest are empty.

exec sp_executesql N'SELECT [AuditName], [DateOfAudit], [DateOfCreation], [Subject], [AuditorName], [Customer], [Territory], [ProfitCenter], [ProjectName], [JobNumber], [Model], [Rating], [MSARating], [Status], [Target] FROM [DRAudit] WHERE [Customer] IN (@p0) AND 1 = 0 /* IN [empty list] / AND 1 = 0 / IN [empty list] / AND 1 = 0 / IN [empty list] / AND 1 = 0 / IN [empty list] / AND 1 = 0 / IN [empty list] / AND 1 = 0 / IN [empty list] / AND 1 = 0 / IN [empty list] */',N'@p0 nvarchar(4000)',@p0=N'Holy & Co.'

Shouldn't the 0 be a 1 in this case when using WhereIn() with Lists/Arrays of strings?

@ahmad-moussawi
Copy link
Contributor

No this is the expected behavior, since the list is empty so the Where Statements is falsy (represented by 1 = 0)
if you want to get results even if one if them is empty you have to use orWhere instead

@gwheeloc
Copy link
Author

gwheeloc commented Jul 8, 2022

Ok, I see. I really didn't want to roll my own Where statement, as the user of the query can pick any of the properties and expects them to be and'd together and ignored if property list is not present.

@ahmad-moussawi
Copy link
Contributor

So you can do check by yourself, by using the When or an explicit if condition.
something like

new Query("Table").Select(dbSelectColumns)
.When(criteria.Customers.Any(), q => q.WhereIn("Customer", criteria.Customers))

@gwheeloc
Copy link
Author

gwheeloc commented Jul 9, 2022

Thank you, this is exactly what I needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants