open System open System.Linq.Expressions type ExprBuilder() = member __.ReturnFrom(f: Expression>) = f member __.Return(x) = __.ReturnFrom(fun _ -> x) let expr = ExprBuilder() let getDateRangePredicate startDate endDate = let dateOf (x: DateTimeOffset) = DateTimeOffset (x.Date, x.Offset) let dayAfter (x: DateTimeOffset) = x.AddDays(1.) let startDate = startDate |> Option.ofNullable |> Option.map dateOf let endDate = endDate |> Option.ofNullable |> Option.map (dayAfter << dateOf) expr { match startDate, endDate with | Some startDate, Some endDate -> return! fun date -> date >= startDate && date < endDate | Some startDate, None -> return! fun date -> date >= startDate | None, Some endDate -> return! fun date -> date < endDate | None, None -> return true } let date1 = Nullable () let date2 = Nullable DateTimeOffset.Now getDateRangePredicate date1 date1 getDateRangePredicate date1 date2 getDateRangePredicate date2 date1 getDateRangePredicate date2 date2