我有以下简单的扩展类
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);
}
Expression.Invoke
是不必要的,但实际上并非如此。 - CSharpie