score:2
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 Query
- Test LINQ to SQL expression
- how to translate a linq expression into sql string use c# code
- How are people unit testing code that uses Linq to SQL
- Convert LINQ Expression to SQL Text without DB Context
- Equivalent of SQL Between Statement Using Linq or a Lambda expression
- Dynamic linq query expression tree for sql IN clause using Entity framework
- Rewriting a LINQ Expression query to enable caching SQL Execution Plan
- LINQ to SQL - best way to switch between test & dev db
- How to reuse a linq expression for 'Where' when using multiple source tables
- LINQ expression is not being translated to SQL
- Expression to get LINQ with Contains to EF for SQL IN() where on entities child's property equals value
- LINQ Source Code Available
- Using a member access lambda expression to parametrise a LINQ to SQL predicate
- Expression in select in LINQ to SQL
- Create Linq Expression for Sql Equivalent "column is null" in c# by creating linq query dynamically
- Embed a test for null in a single LINQ expression
- Convert SQL to linq expression with count
- How to code the partial extensions that Linq to SQL autogenerates?
- Build dynamic linq to Sql lambda expression
- Determine the source DataContext for a Linq to Sql query
- Lambda Expression LINQ Equivalent to SQL Exists Query on Same Table/Variable
- Linq SQL error with one-to-many relationship and orderby complex expression
- How to get SQL query into LINQ form in C# code
- Null checking in LINQ Expression for SQL
- Lambda expression weirdness in a LINQ to SQL 'where' condition
- creating Linq to sqlite dbml from DbLinq source code
- Stubbing Code for Test With Linq Expressions and Lambdas
- Converting Linq expression to sql server query
- Linq to Sql: Optimizing lamba expression - clean code
- Does LINQ convert code to SQL queries
More Query from same tag
- Getting from dynamic Request to a List
- Comparing Datetime? inside Linq Expression
- Linq - Retrieve a single value in a String
- Join anonymous type in LINQ
- How do I convert this LINQ query expression from C# to VB.NET?
- How to format datatable rows data in column wise
- How to count occurences of number stored in file containing multiple delimeters?
- How to divide a LINQ query in small group to avoid Timeout expired exception?
- Web API Dynamic LINQ Search
- Validate file extensions match approved list
- Sharepoint 2010 GetListView Linq XML
- Get N max numbers and their corresponding position(index) from a 2D array double[,] using lambda expression
- Creating groups/blocks from table based on value
- how to use Linq to get a sitecore field
- Why does myDataTable.AsEnumerable().DefaultIfEmpty(myDataTable.NewRow()).First(...) NOT return myDataTable.NewRow() when .First(...) is empty?
- LINQ: Entity string field contains any of an array of strings
- Getting Column Value from another Subquery
- LINQ Joining 2 tables
- Group list of different enums by enum
- How to create a json from database using linq
- How to change a value form ObservableCollection<AnyClass> with LINQ?
- Can LINQ handle a Has-Many-Through relationship (and how is it done)?
- linq query selecting all elements of certain value but none of another value
- Use Last Value in Column for Summary Instead of SUM
- Looking for a data structure that is optimized for finding the next closest element
- LINQ for XML, having trouble reading in multiple elements of varying occurences
- Getting all text nodes in xml using linq or xpath
- What caused this InvalidOperationException using LINQ to SQL?
- Mapping expressions in LINQ-to-sql abstract class
- Update values of a list using Linq