F# 3 SQL 数据库在多列上使用 Type Provider/groupBy

标签 f# type-providers f#-3.0

评估 F# 3 使用类型提供程序功能来替代编写 T/SQL 或存储过程。

let summary = 
  query {   for dsm in db.DistributorSalesMaster do
            join c in db.CustomerMain on 
              ( dsm.CustomerId = c.CustomerId)
            join cal in db.Calendar on
              ( dsm.InvoiceDate =? cal.TheDate)
            join dsd in db.DistributorSalesDetail on
              ( dsm.SalesId = dsd.SalesId)             
            where (dsm.InvoiceDate >= Convert.ToDateTime("2010-12-01") 
                && dsm.InvoiceDate <= Convert.ToDateTime("2011-11-30")
                && c.MainDistributorId=1s
                && c.DistributorId=1s
                && c.CustomerId = 159M
                && cal.APYear?=2011
                )

            groupValBy dsd.InvoiceQuantity ca.APYear into g
            select (g.Key,g.Count())}

它的工作原理与预期的差不多。数据库上下文日志确认了这一点:

    SELECT COUNT(*) AS [Item2], [t2].[APYear] AS [Item1]
    FROM [dbo].[DistributorSalesMaster] AS [t0]
    INNER JOIN [dbo].[CustomerMain] AS [t1] ON [t0].[customerId] = [t1].[customerId]
    INNER JOIN [dbo].[Calendar] AS [t2] ON ([t0].[invoiceDate]) = [t2].[TheDate]
    INNER JOIN [dbo].[DistributorSalesDetail] AS [t3] ON [t0].[salesId] = [t3].[salesId]
    WHERE ([t0].[invoiceDate] >= @p0) AND ([t0].[invoiceDate] <= @p1) AND ([t1].[mainDistributorId] = @p2) AND ([t1].[distributorId] = @p3) AND ([t1].[customerId] = @p4) AND ([t2].[APYear] = @p5)
    GROUP BY [t2].[APYear]
    -- @p0: Input DateTime (Size = -1; Prec = 0; Scale = 0) [12/1/2010 12:00:00 AM]
    -- @p1: Input DateTime (Size = -1; Prec = 0; Scale = 0) [11/30/2011 12:00:00 AM]
    -- @p2: Input SmallInt (Size = -1; Prec = 0; Scale = 0) [1]
    -- @p3: Input SmallInt (Size = -1; Prec = 0; Scale = 0) [1]
    -- @p4: Input Decimal (Size = -1; Prec = 29; Scale = 0) [159]
    -- @p5: Input Int (Size = -1; Prec = 0; Scale = 0) [2011]
    -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.17929

但是,尝试以下操作:

let summary = 
   query {   for dsm in db.DistributorSalesMaster do
             join c in db.CustomerMain on 
                ( dsm.CustomerId = c.CustomerId)
             join cal in db.Calendar on
                ( dsm.InvoiceDate =? cal.TheDate)
             join dsd in db.DistributorSalesDetail on
                ( dsm.SalesId = dsd.SalesId)             
             where (dsm.InvoiceDate >= Convert.ToDateTime("2010-12-01") 
                    && dsm.InvoiceDate <= Convert.ToDateTime("2011-11-30")
                    && c.MainDistributorId=1s
                    && c.DistributorId=1s
                    && c.CustomerId = 159M
                    && cal.APYear?=2011
                    )

             groupValBy dsd.InvoiceQuantity (cal.APYear, cal.APMonth) into g              
             select (g.Key,g.Count())}

生成数据库上下文日志为:

System.NotSupportedException: The member 'System.Tuple`2[System.Nullable`1[System.Int32],System.Nullable`1[System.Int32]].Item1' has no supported translation to SQL.
   at System.Data.Linq.SqlClient.PostBindDotNetConverter.Visitor.VisitMember(SqlMember m)
   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
   .
   .
   .
   at System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.FSharp.Collections.SeqModule.ToArray[T](IEnumerable`1 source)
   at FSI_0002.showDataGrid[a](IEnumerable`1 x) in D:\Work\Tests\FP\FSharpVS2012\FSharpVS2012\IPFS3ZDPTests.fsx:line 21
   at <StartupCode$FSI_0024>.$FSI_0024.main@()
Stopped due to error

我正在尝试对多个列(cal.APYearcal.APMonth)进行groupValBy。它似乎没有通过数据类型提供程序转换为 SQL。

我找到了一些其他方法来实现结果,但数据库上下文日志表明只有连接被转换为 SQL,其余的 groupBy/groupValBy 处理在内存中完成。 这我根本不想要。我希望整个查询表达式在数据库中而不是在内存中进行翻译和执行。希望我清楚地表达了我的追求。

非常感谢这方面的任何帮助或指导。

最佳答案

您必须使用AnonymousObject 。以下内容应该有效:

let summary = 
   query {   for dsm in db.DistributorSalesMaster do
             join c in db.CustomerMain on 
                ( dsm.CustomerId = c.CustomerId)
             join cal in db.Calendar on
                ( dsm.InvoiceDate =? cal.TheDate)
             join dsd in db.DistributorSalesDetail on
                ( dsm.SalesId = dsd.SalesId)             
             where (dsm.InvoiceDate >= Convert.ToDateTime("2010-12-01") 
                    && dsm.InvoiceDate <= Convert.ToDateTime("2011-11-30")
                    && c.MainDistributorId=1s
                    && c.DistributorId=1s
                    && c.CustomerId = 159M
                    && cal.APYear?=2011
                    )

             let key = AnonymousObject<_,_>(cal.APYear, cal.APMonth)

             groupValBy dsd.InvoiceQuantity key into g              
             select (key.Item1, key.Item2, g.Count())}

关于F# 3 SQL 数据库在多列上使用 Type Provider/groupBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13991448/

相关文章:

f# - SqlDataConnection 类型提供程序 - 使用脚本参数设置数据库连接字符串

reflection - F# 类型提供程序引用自定义类型

f# - 使用 F# 中的 System.String.Split

f# - 列表 except - 过滤不等于另一个列表的任何 item.A 的字符串序列

f# - fable websharper borelo SAFE 我应该使用哪个?

f# - F# 中的嵌套联合类型

f# - 为什么 F# 编译器会与 seq{0L..-5L..-10L} 发生冲突?

concurrency - Agent之间如何通信?

f# - 如何让 TypeProviders 在 Xamarin/Monodevelop 上工作

f# - 结合类型测试和文字的模式