如何使用FLinq在F#中进行外部联接? [英] how to outer join in F# using FLinq?

查看:59
本文介绍了如何使用FLinq在F#中进行外部联接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题几乎说明了一切.我有一个以下形式的大flinq查询:

question pretty much says it all. I have a big flinq query of the following form:

for alias1 in table1 do
    for alias2 in table2 do
        if  alias1.Id = alias2.foreignId

使用这种形式,如何在这两个表之间进行左外部联接?

using this form, how can I do a left outer join between these two tables?

推荐答案

我认为您可以使用Query模块中可用的groupJoin函数.这是一个使用Northwind的示例,其中产品"作为主表,类别"作为具有外键的表:

I think you can use the groupJoin function available in the Query module. Here is an example using Northwind with Products as the primary table and Categories as the table with foreign key:

open System.Linq

<@ Query.groupJoin 
     db.Products db.Categories 
     (fun p -> p.CategoryID.Value)
     (fun c -> c.CategoryID)
     (fun p cats ->
        // Here we get a sequence of all categories (which may be empty)
        let cat = cats.FirstOrDefault()
        // 'cat' will be either a Category or 'null' value
        p.ProductName, if cat = null then "(none)" else cat.CategoryName) @>
|> query

使用seq { .. }语法和使用嵌套的for循环实现类似联接的行为,肯定有更好的表达方式.不幸的是,LINQ转换器的报价可能不支持这些报价. (就个人而言,我宁愿使用嵌套的for并使用if来检查空集合来编写代码.)

There are definitely nicer ways of expressing this using the seq { .. } syntax and by implementing join-like behavior using nested for loops. Unfortunatelly, the quotations to LINQ translator will probably not support these. (Personally, I would prefer writing the code using nested for and using if to check for empty collection).

我只是在看PowerPack库中的一些改进,这是F#团队签约工作的一部分,因此希望将来会有所改进...(但没有希望!)

I was just looking at some improvements in the PowerPack library as part of a contracting work for the F# team, so this will hopefully improve in the future... (but no promises!)

这篇关于如何使用FLinq在F#中进行外部联接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆