处理 GroupBy 和 Having
#r @"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.Linq.dll";;
#r @"E:\Projects\F#3\FSharp.Data.TypeProviders.dll";;
open Microsoft.FSharp.Data.TypeProviders;;
open System.Linq;;
// Count()、DefaultIfEmpty 在其中
//open System.Data.Linq;;
//open System.Data.Linq.SqlClient;
// SqlMethods 作其中
[<Generate>]
type T0 = SqlDataConnection<"Data Source=.\SQLEXPRESS;Initial Catalog=FSharpSample;User ID=sa;Password=FSharpSample1234">
let TypeProvider14() =
let db = T0.GetDataContext();
let q = query {
for s in db.Student do
groupBy s.Age into g
where (g.Count()>1)
select (g.Key, g.Count())
}
q |> Seq.iter (fun (key, count) -> printfn "Key=%A Count=%d" key count)
q |> Seq.length
//Key=<null> Count=1
//Key=20 Count=2
//Key=21 Count=2
//Key=22 Count=3
//Key=23 Count=1

本文介绍了一段使用F#进行数据库查询的示例代码,演示了如何利用GroupBy和Having子句来筛选和聚合数据。具体展示了如何设置连接字符串,定义SQLDataConnection类型,并执行LINQ查询来获取年龄分组的学生计数。

被折叠的 条评论
为什么被折叠?



