I am new in F#. So that I face some problem in my project.
let getTotalNoOfSmsPerDay (Db cStr) =
use cmd = new SqlCommandProvider<"select COUNT(id) as NoOfSMS
from transport.SmsNotification
where CONVERT(date, CreatedOn) = CONVERT(date, GETDATE())
",ConnectionString, SingleRow = true>(cStr)
async {
let! result = cmd.AsyncExecute()
return result
}
This is my Code . I just want a integer number NoOfSMS . But It gives me <Option<int>>
How can I solve this?
Thanks in advance
Fatema007
asked Apr 30, 2020 at 5:39
1 Answer 1
Option
represents that nothing may be returned. This is the point where most other languages would return null
.
There are a few ways to use the value inside Option
. Explore the functions available.
Often what you will do though is to match
on the 2 cases ie. Some
value was returned or None
.
match result with
| Some v -> v
| None -> 0
Here are some links to tutorials I have written on the topics.
answered Apr 30, 2020 at 6:07
-
If I want to apply control flow statement in this result it doesn't work. like
let! noOfSMSPerday = connection.GetTotalNoOfSmsPerDay if noOfSMSPerday <= 400 then
It gives error Type mismatch : 'Async<string>' but given a 'Async<unit>'Fatema007– Fatema0072020年04月30日 06:21:55 +00:00Commented Apr 30, 2020 at 6:21 -
If you do not have an else, the return type is unit. Once you have got the int you might want to return from the async CE and just use the value. let n = async { return or return! } depending on what you working with.Devon Burriss– Devon Burriss2020年04月30日 07:28:22 +00:00Commented Apr 30, 2020 at 7:28
-
You are right,As I don't write else it gives me unit . Thank you Solved itFatema007– Fatema0072020年04月30日 08:28:43 +00:00Commented Apr 30, 2020 at 8:28
default