'Evalute part of LINQ expression before translating to SQL statements
Dim instanceOfMyClass As New MyClass()
'... Some code to manipulate instanceOfMyClass.MyArrayField
Dim thisYear = DateTime.Today.Year
Dim records = (
From i In dbContext.MyTable
Where i.MyCol >= thisYear - instanceOfMyClass.MyArrayField.Length AndAlso i.MyCol <= thisYear - 1
).ToArray()
Class MyClass
Friend MyArrayField As String()
End Class
Running above code throws an exception because EF6 doesn't know how to translate instanceOfMyClass.MyArrayField.Length to SQL expression. Of course I can do:
Dim anIntermediateVar = instanceOfMyClass.MyArrayField.Length
Dim records = (
From i In dbContext.MyTable
Where i.MyCol >= thisYear - anIntermediateVar AndAlso i.MyCol <= thisYear - 1
).ToArray()
It just feels a bit unintuitive to me. I wonder if there's a syntax/function/whatever to mark a section of expression so that .NET runtime will evaluate the code within before translating? Something like:
From i In dbContext.MyTable
Where i.MyCol >= thisYear - EvalThisFirst(instanceOfMyClass.MyArrayField.Length) AndAlso i.MyCol <= thisYear - 1
so that when EF6 doing LINQ-to-TSQL translation it's translating from:
From i In dbContext.MyTable
Where i.MyCol >= thisYear - 5 AndAlso i.MyCol <= thisYear - 1
Does such mechanism exist?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
