我有以下簡單的擴展類如何將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'方法仍然不起作用:) –