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

Improper exception on adding a where clause with a call to IList.Contains method with an empty list instance #54

Closed
preslavnd opened this issue Mar 9, 2015 · 2 comments

Comments

@preslavnd
Copy link

When you supply an empty collection, say you have:

var ids = new List();
connection.Query().Where(entity => ids.Contains(entity.Id));

the result is an exception, supplying no valuable information about the error.

PrepareStatement.SetupParameters method assumes there are values in the list, so it tries to remove the trailing comma, which in the case of empty list is non-existant, and raises an exception:

System.ArgumentOutOfRangeException: StartIndex cannot be less than zero. Parameter name: startIndex at System.Text.StringBuilder.Remove(Int32 startIndex, Int32 length) at SqlFu.PrepareStatement.SetupParameters(DbCommand cmd) in e:\Projects.Net\SqlFu\src\SqlFu\PrepareStatement.cs:line 79 at ...

Perhaps the behavior should be either a more informational exception or maybe do what some other ORMs like for instance Entity Framework does, i.e the code emitted when executing the same query using EF:

SELECT
CAST(NULL AS varchar(1)) AS [C1],
CAST(NULL AS bigint) AS [C2],
.......
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
WHERE 1 = 0

Resulting in an empty result set, not an error, or another possible solution maybe not sending SELECT to the SQL server, but just giving back an empty result list.

@sapiens
Copy link
Owner

sapiens commented Mar 9, 2015

I 'm quite busy at the moment, but I can accept a PR into the devel branch

@sapiens
Copy link
Owner

sapiens commented Oct 14, 2015

fixed in ver 2.3.11

@sapiens sapiens closed this as completed Oct 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants