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.
Source: stackoverflow.com
Related Articles
- How to reuse a linq expression for 'Where' when using multiple source tables
- LINQ Source Code Available
- Embed a test for null in a single LINQ expression
- creating Linq to sqlite dbml from DbLinq source code
- Stubbing Code for Test With Linq Expressions and Lambdas
- Linq to Sql: Optimizing lamba expression - clean code
- Test LINQ to SQL expression
- Cannot build the Test project for LINQ IQueryable Toolkit (IQToolkit) - Code 9009
- Unreachable expression code in LINQ query
- source code for LINQ 101 samples
- Linq Expression Refactor Duplicate Code
- I want to implement a linq expression that will allow me to test an property on a supplied object
- optimise code into linq expression
- A LINQ question: map query expression to c# code
- Convert this linq code to expression
- how to translate a linq expression into sql string use c# code
- c# Linq or code to extract groups from a single list of source data
- Saved projection expression for re-use in different linq expressions with two source objects
- EF5 code first datetime2, building a linq expression
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- String.IsNullOrWhiteSpace in LINQ Expression
- How to flatten nested objects with linq expression
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- Linq code to select one item
- "Or" equivalent in Linq Where() lambda expression
- "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities" - stumped!
- Error: "The specified LINQ expression contains references to queries that are associated with different contexts"
- The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
- How are people unit testing code that uses Linq to SQL
- Compare Two Lists Via One Property Using LINQ
- Parsing two consecutive records in XML file
- how to pass local variable to a linq query
- Linq query - List within another list
- Using LinqToCSV - Ignore extra columns?
- Get IP Address on Monotouch/Xamarin in iphone via linq
- How much is there to LINQ?
- Difference in seconds LINQ to Entity (DiffSeconds Usage)
- Duplicate Records when they should be unique
- Exception C# null list
- Group data in DataTable
- Is there a VB.NET-Like operator in C#?
- RavenDb how do I reduce group values into collection in reduce final result?
- LINQ querying nested dictionary with multiple key value selection in C#
- Expression Func with List<T>.Select().Contains()
- Read xml data with namespaces from SQLite database in C#
- C# WinForm Excel check empty sheets function-works but too slow(15 sheets 30 seconds)
- Help to implement a ZipWithRatio extention method
- LINQ Where(), what to do if I need to get everything?
- C# Linq combine multiple values into data row