我不会将SqlDependence设置为LINQ查询 工作代码:
var product = new DataServiceCollection<Product>(target.Products.Expand("DCQuantities").Where(p => p.Sku == "JRSTEST1"));
product.First().DCQuantities[0].AvailableQuantity = 42;
var result = target.SaveChanges();
问题 -
let SetDependency (dc:DataContext) qr fn=
use con=new SqlConnection(dc.Connection.ConnectionString)
con.Open()
use cmd=new SqlCommand(dc.GetCommand(qr).CommandText,con)
let dpn=new SqlDependency(cmd)
let rec remover=lazy(con.Close()
dpn.OnChange.RemoveHandler hnd)
and hnd=new OnChangeEventHandler(fun ob ev->remover.Force()
fn ev)
dpn.OnChange.AddHandler hnd
cmd.ExecuteReader()|>ignore
remover
//SetDependency sql.DataContext q (fun _ ->"Check..."|>Console.WriteLine)
let q=query{for s in sql.Stady do select s} q|>string //or (dc:DataContext).GetCommand(q).CommandText
和好工作;
SELECT [t0].[Id], [t0].[Name], [t0].[PersonId], [t0].[AfterId] FROM [dbo].[Stady] AS [t0]
let q1=query{for s in sql.Stady do select s.Name} q1|>string
和好工作;
SELECT [t0].[Name] FROM [dbo].[Stady] AS [t0]
let q2=query{for s in sql.Stady do select (s.Id,s.Name)} q2|>string (dc:DataContext).GetCommand(q2).CommandText
和
System.Linq.Enumerable+WhereSelectEnumerableIterator'2[Microsoft.FSharp.Linq.RuntimeHelpers.AnonymousObject'2[System.Int64,System.String],System.Tuple'2[System.Int64,System.String]]
P.S。
DataContext.Loq写正确的查询 -
SELECT NULL AS [EMPTY]