处理 Left Join
#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 TypeProvider7() =
let db = T0.GetDataContext()
let q0 = query {
for s in db.Student do
join (for c in db.Course -> s.StudentID = c.CourseID)
select s
}
let q = query {
for e in q0.DefaultIfEmpty() do
select e
}
q |> Seq.iter (fun n -> printfn "%A" n.Name)
q |> Seq.length

本文详细介绍了如何在F#中使用左连接操作,并结合Count()和DefaultIfEmpty方法处理数据。通过生成SqlDataConnection实例连接数据库,展示了一段查询语句的实现,包括如何遍历结果并打印特定字段。
2444

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



