gpt4 book ai didi

sql - F#:如何使用 SQL 数据源创建 Deedle Frame

转载 作者:行者123 更新时间:2023-12-04 17:35:35 33 4
gpt4 key购买 nike

当数据来自 SQL 服务器时,我试图找出在 F# 中创建 Deedle Frame 的最佳方法是什么。我尝试过类似以下的事情。

#I "../packages/Deedle.0.9.12"
#load "Deedle.fsx"

#r "System.dll"
#r "System.Data.dll"
#r "System.Data.Linq"
#r "FSharp.Data.TypeProviders.dll"

open System
open System.Data
open System.Data.Linq
open Microsoft.FSharp.Data.TypeProviders
open Deedle

type dbSchema = SqlDataConnection<"Data Source=server;Initial Catalog=database;Integrated Security=SSPI;">
let db = dbSchema.GetDataContext()

let fr = db.SomeTable |> Frame.ofRows

和其他一些变体。但没有运气。我是 F# 和 Deedle 的新手。
我可以理解为什么上述方法不起作用(Frame.ofRows 与参数不兼容),但我不知道什么是最好的处理方式(甚至如何处理)。

最佳答案

Frame.ofRows函数需要一系列代表帧的各个行的系列。类似于 Frame.ofColumns ,如果您已经有一些系列对象(或者您正在从头开始创建所有内容),则此功能很有用。他们接受类型为 seq<'TRowKey * ISeries<'TColKey>> 的输入.

当你从一些 .NET 数据结构创建 Deedle 框架时,你可以使用 Frame.ofRecords这将适用于任何序列,它将使用反射来获取属性的名称(并将它们视为列名)。

很长的解释,但您的代码中只有几个字符发生了变化:-)。我用 Northwind 对其进行了测试:

type Nwind = SqlDataConnection<"""Data Source=.\SQLExpress;
Initial Catalog=Northwind;Integrated Security=SSPI;""">
let db = Nwind.GetDataContext()

// Create data frame from Products table (with appropriate column names)
let fr = db.Products |> Frame.ofRecords

结果是:

      ProductID ProductName                      SupplierID CategoryID QuantityPerUnit     UnitPrice UnitsInStock UnitsOnOrder ReorderLevel Discontinued OrderDetails                               Categories Suppliers 
0 -> 1 Chai 1 1 10 boxes x 20 bags 18.0000 39 0 10 False System.Data.Linq.EntitySet`1[OrderDetails] Categories Suppliers
1 -> 2 Chang 1 1 24 - 12 oz bottles 19.0000 17 40 25 False System.Data.Linq.EntitySet`1[OrderDetails] Categories Suppliers
2 -> 3 Aniseed Syrup 1 2 12 - 550 ml bottles 10.0000 13 70 25 False System.Data.Linq.EntitySet`1[OrderDetails] Categories Suppliers
3 -> 4 Chef Anton's Cajun Seasoning 2 2 48 - 6 oz jars 22.0000 53 0 0 False System.Data.Linq.EntitySet`1[OrderDetails] Categories Suppliers
4 -> 5 Chef Anton's Gumbo Mix 2 2 36 boxes 21.3500 0 0 0 True System.Data.Linq.EntitySet`1[OrderDetails] Categories Suppliers
(....)

关于sql - F#:如何使用 SQL 数据源创建 Deedle Frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20599075/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com