Skip to content

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

@preslavnd

Description

@preslavnd

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions