LINQ to Entities を使用して、特定の日付期間内のエントリをクエリしています。これは、プロパティを持つモデル オブジェクトに保持されDateFrom
、DateTo
. これを行うには、次を使用してシーケンスを作成します
var allActiveLogs = this.repository.RoutePerformanceLogs
.Where(log => log.Date >= model.DateFrom.Value &&
log.Date <= model.DateTo.Value)
これを抽象化して再利用したい場合は、次の式を作成できます (モデルがスコープ内にある場合)。
Expression<Func<RoutePerformanceLog, bool>> logWithinDateBounds = log =>
log.Date >= model.DateFrom.Value && log.Date <= model.DateTo.Value;
そして後で電話する
var allActiveLogs = this.repository.RoutePerformanceLogs.Where(logWithinDateBounds)
私がやりたいのは、この式をさらに抽象化し、モデルがスコープ外の場所でコード化することです。おそらく署名の式を使用します
Expression<Func<RoutePerformanceLog, DateTime?, DateTime?, bool>> logWithinDateBounds
Func<T, boolean>
ただし、 where メソッドにはまたはが必要なため、これは Where メソッド内では機能しませんExpression<Func<T, boolean>>
。
複数のパラメーターを取り、IQueryable
コレクションをフィルター処理するために使用できる再利用可能な式を作成することは可能ですか (メモリ内のオブジェクトをフィルター処理するのではなく、クエリ プロバイダーを使用してフィルター処理を行うことをお勧めします)。