【发布时间】:2017-04-01 12:48:02
【问题描述】:
我有以下简单的扩展类
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>> 呢?
这就是我尝试使用实体框架的方法
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);
}
【问题讨论】:
标签: c# entity-framework linq lambda expression