I have the following simple extension class
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);
}
}
But Expression.Or returns a BinaryExpression
- How can I get it to return an Expression<Func<T, bool>>
instead?
This is how I am trying to consume the method, using 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);
}