0

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?

3
  • According to EntityFramework Docs: EF 4: This release was included in .NET Framework 4.0 and Visual Studio 2010.... According to What are the correct version numbers for C#?: .NETFramework verion 4.0 was used with C# 4.0. Therefore, it seems if you stick to C# 4.0 syntax, then you won't have any issues. Commented Feb 8 at 19:04
  • The following may be of interest: The history of C# Commented Feb 8 at 19:09
  • Nothing to do with the syntax, it compiles and works, but there is a problem with the translation to SQL Commented Feb 9 at 11:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.