I have a pair of Linq to SQL queries which contain the same complex Where clause, specifically:
where ((range.MinimumFrequency <= minFreq && minFreq <= range.MaximumFrequency)
|| (range.MinimumFrequency <= maxFreq && maxFreq <= range.MaximumFrequency)
|| (range.MinimumFrequency <= minFreq && maxFreq <= range.MaximumFrequency)
|| (range.MinimumFrequency >= minFreq && maxFreq >= range.MaximumFrequency))
And rather than copy and paste this chunk of code all over the place, I wanted to refactor it out into something else that can be shared. I know I can't do this with a normal method as it cannot be translated into SQL, but I also can't get the
Expression < Func<...>>
things described here to work either.
If I simplify the where clause for the purpose of my sanity here, I want to turn
where (range.MinumumFrequency < minFreq)
into an expression, so I tried:
public static Expression<Func<FreqRange, bool>> Overlaps(decimal minMHz, decimal maxMHz)
{
return (range => range.MinimumFrequency <= minMHz);
}
This appears to compile, but I can't seem to get the where statement to work, I have tried the following:
where FreqRange.Overlaps(minMHz, maxMHz)
but this gives me a compile time error:
Cannot implicitly convert type 'System.Linq.Expressions.Expression>' to 'bool'
Any ideas? Also, assuming we get this working, can I simply extend the lambda expression in the Expression < Func <>> to include the other conditions?