ServiceStack/ServiceStack.OrmLite — pull request #334 by schmidt4brains (2013-10-17 01:30:40+00:00)
Title: Avoid generating invalid SQL when a LINQ Contains() expressions references an empty collection.

When a LINQ Contains() expression references an empty collection, `SqlExpressionVisitor` generates invalid SQL of the form `ColumnName IN ()`. All the SQL parsers complain about those two parentheses with nothing in between.

Consider this code:

class Person
{
public int Id { get; set; }
public string Name {get; set; }
}

var personIds = new List();

// Some code that populates personIds with IDs of the rows you want to select
...

// This query will cause a SQL syntax error if personIds is an empty collection
var persons = Db.Select(person => personIds.Contains(person.Id));

That last line will generate `SELECT "Id", "Name" FROM "Person" WHERE "Id" In ()` and throw an exception.

So currently, any code that uses a LINQ `Contains()` method needs to be guarded with a clumsy clause:

var persons = new List();

if( personIds.Any() )
{
persons = Db.Select(person => personIds.Contains(person.Id));
}

That isn't very wrist-friendly. :(

With this commit, any time the collection referenced by a `Contains()` method is empty, the generated SQL clause will be `(NULL)` instead of `()`, which is semantically the same thing, but valid SQL.

Now the one-liner query:

var persons = Db.Select(person => personIds.Contains(person.Id));

will return an empty list as expected, without throwing an exception.