score:2

Accepted answer

Instead of

   result.FirstOrDefault(); 

would it be sufficient to use

    string sqlCommand = dataContext.GetCommand(result).CommandText; 

?

If the expression does not generate valid Sql, this should throw a NotSupportedException, but it does not actually execute the sqlCommand.

score:1

I think this will solve your problem:

IQueryable<TEntity> table = GetTable<TEntity>();  
IQueryable<TEntity> result;
try
{
    return table.Where(searchExpression).ToList();
}
catch (NotSupportedException)
{
    //trying to perform in-memory search if query could not be constructed
    return table
        .AsEnumerable()
        .Where(searchExpression.Compile())
        .ToList();
}

So the method returns is the expression is converted to valid SQL. Otherwise it catches the exception and runs the query in memory. This should work but it doesn't answer your question if it's possible to check if a specific searchExpression can be converted. I don't think such a thing exists.


Related Query

More Query from same tag