FirstOrDefault In F#

前端 未结 4 1074
孤街浪徒
孤街浪徒 2021-01-17 12:27

How to write FirstOrDefault Linq Query in F#? Can I use linq to sql in F# in totally?

4条回答
  •  太阳男子
    2021-01-17 12:59

    Alternatively, you can define your own firstordefault easily:

    let firstordefault list =
       match list with
       | head :: tail -> head 
       | [] -> 0 // some default value
    

    Example:

    let exampleList = [ 1; 2; 3 ]
    

    using F# interactive,

    firstordefault exampleList;;  
    val it : int = 1
    

提交回复
热议问题