我有以下简单的扩展类如何将BinaryExpression转换为表达式<Func <T, bool>>?
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);
}
如果您使用LINQKit,整个问题没有任何意义,因为有问题的功能是由LINQKit提供的。 –
这就是为什么LinqKit在答案中提到,而不是在问题中,因为我曾经解决问题:) –
不,你没有。问题是关于你的“Or”扩展方法,你“回答”与这个问题没有任何共同之处。我的意思是,有问题的'Or'方法仍然不起作用:) –