F#从查询表达式转换类型
我有一个如下形式的查询表达式:F#从查询表达式转换类型,f#,F#,我有一个如下形式的查询表达式: let result = query { for row in context.Table do where (row.id = 111111) select (row.col1,row.col2,row.col3) } 结果返回类型为IQueryable的值。我希望它返回seq*seq*seq 我可以试着这样改变它: let result :seq<float>*s
let result = query { for row in context.Table do
where (row.id = 111111)
select (row.col1,row.col2,row.col3) }
结果返回类型为IQueryable的值。我希望它返回seq*seq*seq
我可以试着这样改变它:
let result :seq<float>*seq<float>*seq<float> = query { for row in context.Table do
where (row.id = 111111)
select (row.col1,row.col2,row.col3) }
|> Seq.cast
let result:seq*seq*seq=query{for context.Table do中的行
其中(row.id=111111)
选择(row.col1,row.col2,row.col3)}
|>序号
但我得到:
类型不匹配。应为IQueryable->seq*seq*seq
但是给出一个IQueryable->seq我和Mark一样,我想知道你想用这个来完成什么,但是,这里有一种方法可以做到:
open System
open System.Linq
// Helpers to recreate your circumstances.
type Context = { id : Int32; col1 : Nullable<Double>; col2 : Nullable<Double>; col3 : Nullable<Double>}
let context = Unchecked.defaultof<IQueryable<Context>>
let result = query { for row in context do
where (row.id = 111111)
select (row.col1,row.col2,row.col3) }
let seqTuple =
result
|> Seq.fold (fun (col1s, col2s, col3s) (col1, col2, col3) ->
(if col1.HasValue then col1.Value :: col1s else col1s),
(if col2.HasValue then col2.Value :: col2s else col2s),
(if col3.HasValue then col3.Value :: col3s else col3s)
) ([], [], [])
|> fun (col1s, col2s, col3s) ->
List.rev col1s,
List.rev col2s,
List.rev col3s
开放系统
开放系统
//帮助您重新创建您的环境。
类型上下文={id:Int32;col1:Nullable;col2:Nullable;col3:Nullable}
让context=Unchecked.defaultof
让result=query{for context do中的行
其中(row.id=111111)
选择(row.col1,row.col2,row.col3)}
让seqTuple=
结果
|>顺序折叠(乐趣(第一栏、第二栏、第三栏)(第一栏、第二栏、第三栏)->
(如果col1.HasValue,则col1.Value::col1s,否则为col1s),
(如果col2.HasValue,则col2.Value::col2s else col2s),
(如果是col3.HasValue,则是col3.Value::col3s,否则为col3s)
) ([], [], [])
|>乐趣(酷玩、酷玩、酷玩)->
List.rev col1s,
List.rev col2s,
List.rev col3s
我和马克一样,想知道你想用这个来完成什么,但是,这里有一种方法可以做到:
open System
open System.Linq
// Helpers to recreate your circumstances.
type Context = { id : Int32; col1 : Nullable<Double>; col2 : Nullable<Double>; col3 : Nullable<Double>}
let context = Unchecked.defaultof<IQueryable<Context>>
let result = query { for row in context do
where (row.id = 111111)
select (row.col1,row.col2,row.col3) }
let seqTuple =
result
|> Seq.fold (fun (col1s, col2s, col3s) (col1, col2, col3) ->
(if col1.HasValue then col1.Value :: col1s else col1s),
(if col2.HasValue then col2.Value :: col2s else col2s),
(if col3.HasValue then col3.Value :: col3s else col3s)
) ([], [], [])
|> fun (col1s, col2s, col3s) ->
List.rev col1s,
List.rev col2s,
List.rev col3s
开放系统
开放系统
//帮助您重新创建您的环境。
类型上下文={id:Int32;col1:Nullable;col2:Nullable;col3:Nullable}
让context=Unchecked.defaultof
让result=query{for context do中的行
其中(row.id=111111)
选择(row.col1,row.col2,row.col3)}
让seqTuple=
结果
|>顺序折叠(乐趣(第一栏、第二栏、第三栏)(第一栏、第二栏、第三栏)->
(如果col1.HasValue,则col1.Value::col1s,否则为col1s),
(如果col2.HasValue,则col2.Value::col2s else col2s),
(如果是col3.HasValue,则是col3.Value::col3s,否则为col3s)
) ([], [], [])
|>乐趣(酷玩、酷玩、酷玩)->
List.rev col1s,
List.rev col2s,
List.rev col3s
我和马克一样,想知道你想用这个来完成什么,但是,这里有一种方法可以做到:
open System
open System.Linq
// Helpers to recreate your circumstances.
type Context = { id : Int32; col1 : Nullable<Double>; col2 : Nullable<Double>; col3 : Nullable<Double>}
let context = Unchecked.defaultof<IQueryable<Context>>
let result = query { for row in context do
where (row.id = 111111)
select (row.col1,row.col2,row.col3) }
let seqTuple =
result
|> Seq.fold (fun (col1s, col2s, col3s) (col1, col2, col3) ->
(if col1.HasValue then col1.Value :: col1s else col1s),
(if col2.HasValue then col2.Value :: col2s else col2s),
(if col3.HasValue then col3.Value :: col3s else col3s)
) ([], [], [])
|> fun (col1s, col2s, col3s) ->
List.rev col1s,
List.rev col2s,
List.rev col3s
开放系统
开放系统
//帮助您重新创建您的环境。
类型上下文={id:Int32;col1:Nullable;col2:Nullable;col3:Nullable}
让context=Unchecked.defaultof
让result=query{for context do中的行
其中(row.id=111111)
选择(row.col1,row.col2,row.col3)}
让seqTuple=
结果
|>顺序折叠(乐趣(第一栏、第二栏、第三栏)(第一栏、第二栏、第三栏)->
(如果col1.HasValue,则col1.Value::col1s,否则为col1s),
(如果col2.HasValue,则col2.Value::col2s else col2s),
(如果是col3.HasValue,则是col3.Value::col3s,否则为col3s)
) ([], [], [])
|>乐趣(酷玩、酷玩、酷玩)->
List.rev col1s,
List.rev col2s,
List.rev col3s
我和马克一样,想知道你想用这个来完成什么,但是,这里有一种方法可以做到:
open System
open System.Linq
// Helpers to recreate your circumstances.
type Context = { id : Int32; col1 : Nullable<Double>; col2 : Nullable<Double>; col3 : Nullable<Double>}
let context = Unchecked.defaultof<IQueryable<Context>>
let result = query { for row in context do
where (row.id = 111111)
select (row.col1,row.col2,row.col3) }
let seqTuple =
result
|> Seq.fold (fun (col1s, col2s, col3s) (col1, col2, col3) ->
(if col1.HasValue then col1.Value :: col1s else col1s),
(if col2.HasValue then col2.Value :: col2s else col2s),
(if col3.HasValue then col3.Value :: col3s else col3s)
) ([], [], [])
|> fun (col1s, col2s, col3s) ->
List.rev col1s,
List.rev col2s,
List.rev col3s
开放系统
开放系统
//帮助您重新创建您的环境。
类型上下文={id:Int32;col1:Nullable;col2:Nullable;col3:Nullable}
让context=Unchecked.defaultof
让result=query{for context do中的行
其中(row.id=111111)
选择(row.col1,row.col2,row.col3)}
让seqTuple=
结果
|>顺序折叠(乐趣(第一栏、第二栏、第三栏)(第一栏、第二栏、第三栏)->
(如果col1.HasValue,则col1.Value::col1s,否则为col1s),
(如果col2.HasValue,则col2.Value::col2s else col2s),
(如果是col3.HasValue,则是col3.Value::col3s,否则为col3s)
) ([], [], [])
|>乐趣(酷玩、酷玩、酷玩)->
List.rev col1s,
List.rev col2s,
List.rev col3s
这个问题分为两部分:
- 将
转换为seq
:seq*seq*seq
和列表。unzip3
正是这样做的数组。unzip3
- 摆脱null:这取决于当值为null时希望发生什么
- 如果要为空值返回0:
let col1, col2, col3 = query { for row in context.Table do where (row.id = 111111) let col1 = if row.col1.HasValue then row.col1.Value else 0. let col2 = if row.col2.HasValue then row.col2.Value else 0. let col3 = if row.col3.HasValue then row.col3.Value else 0. select (col1, col2, col3) } |> Array.ofSeq |> Array.unzip3
- 如果要忽略存在空值的行:
let col1, col2, col3 = query { for row in context.Table do where (row.id = 111111 && row.col1.HasValue && row.col2.HasValue && row.col3.HasValue) select (row.col1.Value, row.col2.Value, row.col3.Value) } |> Array.ofSeq |> Array.unzip3
- 如果要为空值返回0:
- 将
转换为seq
:seq*seq*seq
和列表。unzip3
正是这样做的数组。unzip3
- 摆脱null:这取决于当值为null时希望发生什么
- 如果要为空值返回0:
let col1, col2, col3 = query { for row in context.Table do where (row.id = 111111) let col1 = if row.col1.HasValue then row.col1.Value else 0. let col2 = if row.col2.HasValue then row.col2.Value else 0. let col3 = if row.col3.HasValue then row.col3.Value else 0. select (col1, col2, col3) } |> Array.ofSeq |> Array.unzip3
- 如果要忽略存在空值的行:
let col1, col2, col3 = query { for row in context.Table do where (row.id = 111111 && row.col1.HasValue && row.col2.HasValue && row.col3.HasValue) select (row.col1.Value, row.col2.Value, row.col3.Value) } |> Array.ofSeq |> Array.unzip3
- 如果要为空值返回0:
- 将
转换为seq
:seq*seq*seq
和列表。unzip3
正是这样做的数组。unzip3
- 摆脱null:这取决于当值为null时希望发生什么
- 如果要为空值返回0:
let col1, col2, col3 = query { for row in context.Table do where (row.id = 111111) let col1 = if row.col1.HasValue then row.col1.Value else 0. let col2 = if row.col2.HasValue then row.col2.Value else 0. let col3 = if row.col3.HasValue then row.col3.Value else 0. select (col1, col2, col3) } |> Array.ofSeq |> Array.unzip3
- 如果要忽略存在空值的行:
let col1, col2, col3 = query { for row in context.Table do where (row.id = 111111 && row.col1.HasValue && row.col2.HasValue && row.col3.HasValue) select (row.col1.Value, row.col2.Value, row.col3.Value) } |> Array.ofSeq |> Array.unzip3
- 如果要为空值返回0:
- 将
转换为seq
:seq*seq*seq
和列表。unzip3
正是这样做的数组。unzip3
- 摆脱null:这取决于当值为null时希望发生什么
- 如果要为空值返回0:
let col1, col2, col3 = query { for row in context.Table do where (row.id = 111111) let col1 = if row.col1.HasValue then row.col1.Value else 0. let col2 = if row.col2.HasValue then row.col2.Value else 0. let col3 = if row.col3.HasValue then row.col3.Value else 0. select (col1, col2, col3) } |> Array.ofSeq |> Array.unzip3
- 如果要忽略存在空值的行:
let col1, col2, col3 = query { for row in context.Table do where (row.id = 111111 && row.col1.HasValue && row.col2.HasValue && row.col3.HasValue) select (row.col1.Value, row.col2.Value, row.col3.Value) } |> Array.ofSeq |> Array.unzip3
- 如果要为空值返回0:
- 这个问题分为两部分:
- 这个问题分为两部分:
- 这个问题分为两部分:
seq*seq*seq
?你期望它会怎么样