1

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

asked Apr 30, 2020 at 5:39

1 Answer 1

5

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.

Control flow
Pattern matching
Handling no data

answered Apr 30, 2020 at 6:07
3
  • 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>' Commented 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. Commented Apr 30, 2020 at 7:28
  • You are right,As I don't write else it gives me unit . Thank you Solved it Commented Apr 30, 2020 at 8:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.