如何将BinaryExpression转换为表达式<func <t,bool =“”>&gt;?

时间:2017-04-01 12:48:03

标签: c# entity-framework linq lambda expression

我有以下简单的扩展类

public static class ExpressionOrExtension
{
    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> source, Expression<Func<T, bool>> expression)
    {
        if (source == null)
            return expression;
        return Expression.Or(source, expression);
    }
}

但Expression.Or会返回BinaryExpression - 如何才能让它返回Expression<Func<T, bool>>

这就是我尝试使用Entity Framework

来使用该方法的方法
    public IQueryable<BookVerse> FindByVerseReferences(string bookCode, params VerseReference[] verseReferences)
    {
        Expression<Func<BookVerse, bool>> conditions = null;
        foreach(VerseReference verseReference in verseReferences ?? new VerseReference[0])
        {
            conditions = conditions.Or<BookVerse>(x =>
                    x.BookCode == bookCode
                    && x.Chapter == verseReference.Chapter
                    && x.FirstVerse <= verseReference.LastVerse
                    && x.LastVerse >= verseReference.FirstVerse);
        }
        return MyDbContext.BookVerses.Where(conditions);
    }

2 个答案:

答案 0 :(得分:3)

您需要构建一个lambda表达式:

var p = Expression.Parameter(typeof(T));
return (Expression<Func<T,bool>>)Expression.Lambda(
    Expression.Or(
        Expression.Invoke(source, p)
    ,   Expression.Invoke(expression, p)
    )
,   p
);

Demo:

Expression<Func<int,bool>> a = x=>x > 5;
Expression<Func<int,bool>> b = x=>x < -5;
var or = Or(a, b);
var f = (Func<int,bool>)or.Compile();
for (int i = -10 ; i <= 10 ; i++) {
    Console.WriteLine("{0} - {1}", i, f(i));
}

答案 1 :(得分:2)

我使用了LinqKit nuget包并实现了它......

    public IQueryable<BookVerse> FindByVerseReferences(string bookCode, params VerseReference[] verseReferences)
    {
        var predicateBuilder = PredicateBuilder.New<BookVerse>();
        Expression<Func<BookVerse, bool>> predicate = null;
        foreach(VerseReference verseReference in verseReferences ?? new VerseReference[0])
        {
            Expression<Func<BookVerse, bool>> conditions = (x =>
                    x.BookCode == bookCode
                    && x.Chapter == verseReference.Chapter
                    && x.FirstVerse <= verseReference.LastVerse
                    && x.LastVerse >= verseReference.FirstVerse);
            predicate = predicateBuilder.Or(conditions);
        }
        return ObjectSpace.BookVerses.AsExpandable().Where(predicate);
    }