F#:如何使用SQL数据源创建Deedle框架 [英] F#: How to create a Deedle Frame with SQL data source

查看:91
本文介绍了F#:如何使用SQL数据源创建Deedle框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚当数据来自SQL Server时,F#中创建Deedle Frame的最佳方法是什么。我已经尝试过以下操作。

I am trying to figure what is the best way in F# to create a Deedle Frame, when the data comes from an SQL server. I have tried things like the following.

#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与参数不兼容),但是我不知道什么是进行(或进行)的最佳方法。

and a few other variants. But without luck. I am new to both F# and Deedle for that matter. I can see why the above does not work (the Frame.ofRows is not compatible with the argument) but I don't know what is the best way to proceed (or even how to proceed).

推荐答案

Frame.ofRows 函数需要一个表示序列的单独序列的序列帧。与 Frame.ofColumns 相似,如果您已经有一些系列对象(或者从头开始创建所有对象),则此函数很有用。他们接受类型为 seq<'TRowKey * ISeries<'TColKey>> 的输入。

The Frame.ofRows function expects a sequence of series that represent individual rows of the frame. Similarly to Frame.ofColumns, this function is useful if you already have some series objects (or if you are creating everything from scratch). They take input of type seq<'TRowKey * ISeries<'TColKey>>.

从某些.NET数据结构创建Deedle框架,可以使用 Frame.ofRecords ,该框架可在任何序列上工作,并且将使用反射来获取属性的名称(并它们作为列名)。

When you're creating Deedle frame from some .NET data structure, you can use Frame.ofRecords which will work on any sequence and it will use reflection to get the names of the properties (and treat them as column names).

很长的解释,但是您的代码中只有几个字符改变了:-)。我用罗斯文(Northwind)进行了测试:

A long explanation, but just a few characters change in your code :-). I tested it with 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 
(....)

这篇关于F#:如何使用SQL数据源创建Deedle框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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