leftOuterJoin
MSDN 上查询表达式的文档通过示例反复暗示,在使用时leftOuterJoin .. on .. into ..
您仍然必须使用它.DefaultIfEmpty()
才能达到预期的效果。
我不认为这是必要的,因为我在这两个测试中得到了相同的结果,不同之处仅在于第二个测试没有.DefaultIfEpmty()
type Test = A | B | C
let G = [| A; B; C|]
let H = [| A; C; C|]
printfn "%A" <| query {
for g in G do
leftOuterJoin h in H on (g = h) into I
for i in I.DefaultIfEmpty() do
select (g, i)}
printfn "%A" <| query {
for g in G do
leftOuterJoin h in H on (g = h) into I
for i in I do
select (g, i)}
// seq [(A, A); (B, null); (C, C); (C, C)]
// seq [(A, A); (B, null); (C, C); (C, C)]
1)你能确认一下吗?
如果那是正确的,我只是在编写了这个替代类型增强以更好地处理不匹配的结果之后才意识到这一点,我很惊讶null
在我的输出中仍然看到 s !
type IEnumerable<'TSource> with
member this.NoneIfEmpty = if (Seq.exists (fun _ -> true) this)
then Seq.map (fun e -> Some e) this
else seq [ None ]
printfn "%A" <| query {
for g in G do
leftOuterJoin h in H on (g = h) into I
for i in I.NoneIfEmpty do
select (g, i)}
// seq [(A, Some A); (B, Some null); (C, Some C); (C, Some C)]
2)有没有办法None
代替null
/Some null
从leftOuterJoin
?
3)我真正想做的是找出是否有任何不匹配的g
printfn "%A" <| query {
for g in G do
leftOuterJoin h in H on (g = h) into I
for i in I.NoneIfEmpty do
where (i.IsNone)
exists (true) }
我想出了下一个,但它不是很 F#:
printfn "%A" <| query {
for g in G do
leftOuterJoin h in H on (g = h) into I
for i in I do
where (box i = null)
exists (true)}