I am trying to combine multiple LINQ expressions as an OR condition, so that they can be used in Entity Framework 4. The upgrade is not an option here. The VB code I have is:
Public Function CombineExpressions(Of T)(ParamArray filters() As Expression(Of Func(Of T, Boolean))) As Expression(Of Func(Of T, Boolean))
Dim firstFilter = filters?.FirstOrDefault()
If firstFilter Is Nothing Then
Dim alwaysTrue As Expression(Of Func(Of T, Boolean)) = Function(x) True
Return alwaysTrue
End If
Dim body = firstFilter.Body
Dim param = firstFilter.Parameters.ToArray()
For Each nextFilter In filters.Skip(1)
Dim nextBody = Expression.Invoke(nextFilter, param)
body = Expression.OrElse(body, nextBody)
Next
Dim result = Expression.Lambda(Of Func(Of T, Boolean))(body, param)
Return result
End Function
When I run this I get an exception about Invoke
not being supported by LINQ to Entities. If I remove the Invoke
expression, I get an error about OrElse
not being defined for two lambdas returning Func(Of T, Boolean)
. This code does work in EF Core, but I am unable to migrate, so that is not an option.
What am I missing?