最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - Dynamically Combining LINQ Expressions for Entity Framework 4 - Stack Overflow

programmeradmin1浏览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?

发布评论

评论列表(0)

  1. 暂无评论