Types F查询,按单列将多个值分组

Types F查询,按单列将多个值分组,types,f#,provider,Types,F#,Provider,我有一个F sql查询,它需要对每个组中的两列求和 let financials = query{ for data in dbData do groupValBy (data.earning, data.losses) data.store into group select (group.Key, group.Sum(fun (group) -> fst group), group.Sum(fun (group) -> snd gr

我有一个F sql查询,它需要对每个组中的两列求和

let financials = query{
        for data in dbData do
        groupValBy (data.earning, data.losses) data.store into group
        select (group.Key, group.Sum(fun (group) -> fst group), group.Sum(fun (group) -> snd group))
    }
我还没有找到任何例子来说明如何做到这一点,但是这段代码编译得很好,而且根据各种迹象,它应该可以工作。但是,当执行时,我总是会收到一个异常消息,无法将节点“New”格式化为SQL执行。如果删除data.loss,一切正常,但添加第二个值会导致错误。还有别的办法吗?我们将不胜感激

注意,此问题类似,但处理多个列分组的相反情况:

更新:

我还尝试了以下方法:

(NewAnonymousObjectHelper(T(data.earning, data.losses)))
式中,T为:

type T(earning : decimal, losses : decimal) =
    member val Earning=earning with get,set
    member val Losses=losses with get,set
但是,需要创建一个新的Sum函数来接受类型T。我还尝试使用F PowerPack中的MutableTuple,但未能让它识别MutableTuple,因为它不太熟悉此代码/dll和相关名称空间。但它似乎应该起作用:

let t = MutableTuple<_,_>(item1=data.earning,item2=data.losses)
groupValBy t data.store into group
也许有人能给我一个建议,我在这里遗漏了什么

结论:


看起来在选择之前使用let对结果求和是可行的方法。不清楚为什么group.Sum在选择行上不起作用。注意:我上面提到的错误消息是由一些sql server问题引起的。

尽管我不得不承认,我还没有测试过这一点-我有一种感觉,类似这样可能会起作用:

let financials =
    query {
        for data in dbData do
        groupValBy (data.earning, data.losses) data.store into group
        let earnings = query { for (e,_) in group do sumBy e }
        let losses = query { for (_,l) in group do sumBy l }
        select (group.Key, earnings, losses) }

受上的示例启发。您可能希望改用sumByNullable。

这可能是另一个仅使用groupBy的解决方案,但仅使用linq to对象对其进行了测试

type Data =
    {Store : string; Earning : float; Losses : float }

let dbData =
    [| { Store = "Store A"; Earning = 5.0; Losses = 2.0 }
       { Store = "Store A"; Earning = 10.0; Losses = 3.0 }
       { Store = "Store B"; Earning = 3.0; Losses = 1.0 } |]

let financials =
    query {
        for data in dbData do
        groupBy data.Store into group
        let earnings = query { for g in group do sumBy g.Earning }
        let losses = query { for g in group do sumBy g.Losses }
        select (group.Key, earnings, losses) } |> Seq.toArray

我想你需要将参数data.store和data.earning,data.loss像groupValBy data.earning,data.loss data.store一样切换到groupValBy中。在谷歌上快速搜索后,我发现你可能是对的。如我所说,我没有测试它。我已经修改了我的建议。我现在应该做这项工作。是的,这确实有效。我只是不明白为什么group.Sumfun e、u->e代替选择行上的收益不起作用。感谢您的帮助…当我将GOP的原始程序连接到数据对象而不是SQL数据库时,它对我很有用。我想先解释一下为什么SQL会出现问题,以及您的解决方案如何帮助解决问题?