public IQueryable <ActivityView> Handle(ActivitiesByDateRange query) { if (query == null) { throw new ArgumentNullException("query"); } IQueryable <ActivityView> activityViews = new EnumerableQuery <ActivityView>(); try { var possibleNullView = _projector.BeginReadView(); if (possibleNullView != null) { var view = possibleNullView.AsQueryable(); activityViews = view.Where(a => ( /* and, include activities that are undated... */ (!query.NoUndated || (!a.StartsOn.HasValue && !a.EndsOn.HasValue && (!a.OnGoing.HasValue || !a.OnGoing.Value))) && /* and */ ( /* On-going only */ (!a.StartsOn.HasValue && !a.EndsOn.HasValue && (a.OnGoing.HasValue && a.OnGoing.Value)) || /* End date only (with future flag) */ (!a.StartsOn.HasValue && (a.EndsOn.HasValue && (a.EndsOn.Value >= query.FromDate) && (query.IncludeFuture || (a.EndsOn < query.ToDate))) && (!a.OnGoing.HasValue || !a.OnGoing.Value)) || /* Start date and on-going */ ((a.StartsOn.HasValue && (a.StartsOn.Value < query.ToDate)) && !a.EndsOn.HasValue && (a.OnGoing.HasValue && a.OnGoing.Value)) || /* Start date only */ ((a.StartsOn.HasValue && ((a.StartsOn.Value >= query.FromDate) && (a.StartsOn.Value < query.ToDate))) && !a.EndsOn.HasValue && (!a.OnGoing.HasValue || !a.OnGoing.Value)) || /* Start date and end date (with future flag) */ ((a.StartsOn.HasValue && (a.StartsOn.Value >= query.FromDate))) && (a.EndsOn.HasValue && (query.IncludeFuture || (a.EndsOn < query.ToDate))) && (!a.OnGoing.HasValue || !a.OnGoing.Value)) ) ); } } finally { _projector.EndReadView(); } return(activityViews.AsQueryable()); }