There is a standard query operator headOrDefault
which returns the first result or default value. Sadly, the default value of tuple will be null
(I think), so this will be unsafe. You could return an option type, because the default value of an option type is None
:
let query =
query { for row in db.MyTable do
where (row.ID = 1)
select (Some(row.Col1, row.Col2))
headOrDefault }
Alternatively, you can use this nice extension which adds headOrNone
that does exactly what you want - returns Some(col1, col2)
if there are some rows or None
otherwise:
let query =
query { for row in db.MyTable do
where (row.ID = 1)
select (Some(row.Col1, row.Col2))
headOrNone }