The lastOrDefault returns the last date if the data table is not empty. Otherwise, the default value of DateTime is returned, which is DateTime.MinValue .
Since you cannot change this default value, it is better to check if the query result is the default value and returns outside the query limits:
let lastDate = let d = query { for day in days do lastOrDefault } if d = DateTime.MinValue then new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) else d
However, the return of Unix Epoch is not an F # -shaped way to handle exceptional cases. It is clearer to return None if there is no record in the data, and it is also easier to process this option value to eliminate an unexpected situation:
let lastDate = let d = query { for day in days do lastOrDefault } if d = DateTime.MinValue then None else Some d match lastDate with | None -> (* Process the exceptional case *) | Some d -> (* Do something with d *)
source share