score:2
Accepted answer
Try refactoring the IsNullOrEmpty condition like this:
return (from r in Repository.Query<Measurement>()
where
(string.IsNullOrEmpty(postalCode)
|| r.Postal.ToLowerInvariant() == postalCode.ToLowerInvariant()
)
&&
(string.IsNullOrEmpty(trait)
|| r.Trait.ToLowerInvariant() == trait.ToLowerInvariant()
)
select r).ToList();
That may cause LINQ to evaluate the IsNullOrEmpty before sending off the query. If not, you could precalculate them manually and put a couple boolean variables in their place.
score:1
Have you tried forcing the deferred execution by calling Repository.Query().ToList()
before the where clauses? I noticed it looks like NHibernate is attempting to convert the string.IsNullOrEmpty()
call into SQL syntax (and failing).
return (from r in Repository.Query<Measurement>().ToList()
where
r.Postal.ToLowerInvariant() ==
(string.IsNullOrEmpty(postalCode)
? r.Postal : postalCode).ToLowerInvariant()
&&
r.Trait.ToLowerInvariant() ==
(string.IsNullOrEmpty(trait)
? r.Trait : trait).ToLowerInvariant()
select r).ToList();
Source: stackoverflow.com
Related Articles
- Make Linq to Sql generate T-SQL with ISNULL instead of COALESCE
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- IsNull or Coalesce functionality in LINQ?
- Linq ISNULL functionality
- source code for LINQ 101 samples
- How to get same functionality as ISNULL in LINQ query
- Using LINQ Expressions to Multiple Left Join and use ISNULL Functionality
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- How are people unit testing code that uses Linq to SQL
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- How to detect IsNull / NotNull when building dynamic LINQ expressions?
- Syntax to execute code block inside Linq query?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Best open source LINQ provider
- Is there a good source that gives an overview of linq optimizations?
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- LINQ WHERE method alters source collection
- Where can I view LINQ source code?
- Suggestions for designing complex LINQ code
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Left outer join using LINQ -- understanding the code
- How to pass LinQ Expressions from F# to C# code
- How to reuse a linq expression for 'Where' when using multiple source tables
- C# Null coalesce with LINQ
- Avoiding code repetition when using LINQ
- How to get all elements out of multiple List<>?
- Most efficient way to choose an item from a list with a given probability
- why we need to use AsEnumerable() method?
- select from grouped collection
- How to write dynamic where clause for join range varible
- Filter list from database using in-memory list
- Where Clause is not applied when converting the query from LINQ (EF) to SQL
- Group by in a dictionary<string, IList<string>>
- Duplicates In DataTable, Getting Last By Specifying Two Properties
- Assign list after join
- Dynamically setting LINQ datasource results in Operator '==' incompatible with operand types 'String' and 'Int32'
- Group by and then select based on those group values
- Compare two lists and find duplicate records that exist in both
- LINQ querying nested dictionary with multiple key value selection in C#
- Getting data from xml by attribute vlaue
- Intersect with a custom IEqualityComparer using Linq
- Making date comparison work with different language settings at DB and Application
- How to delete objects from array using LINQ
- Why does this anonymous type Linq expression work?
- How to fill data in nested Lists based on Ids in C#?