参数LINQ查询 [英] Parametric LINQ query

查看:110
本文介绍了参数LINQ查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对访问F#中的动态对象的另一种看法 let y = x.Where(fun x -> x.City ="London").Select("new(City,Zip)")参数化查询并提取必要的项目.这些将与SQL查询中的列相对应,并由datacontext的属性表示.这是我想作为参数传递的部分.

This is another take on accessing dynamic objects in F# There I'm using let y = x.Where(fun x -> x.City ="London").Select("new(City,Zip)") to parametrize the query and extract the necessary items. These would correspond to columns in an SQL query, and be represented by a property of the datacontext. This is the part that I would like to pass in as a parameter.

type Northwind = ODataService<"http://services.odata.org/Northwind/Northwind.svc">
let db = Northwind.GetDataContext()
let query2 = query { for customer in db.Customers do
                     select customer}  |> Seq.toArray
let qryfun (x:Northwind.ServiceTypes.Customer) =
    query { for x in query2 do
            select (x.City,x.CompanyName,x.Country)}

基本上,我不仅要传递x,而且要传递x.*.当我访问一个固定的数据库时,我可以分解出x.但是,我现在有40个提取不同列的小函数.是否可以将其分解为一个函数并将该属性作为参数传递?因此有时我提取x.City,而另一些时间提取x.Country.我尝试使用引号,但是无法正确拼接,也许这不是正确的方法.

Basically I would like to pass in not only x but also x.*. As I'm accessing one database that is fixed, I can factor out x. However I now have 40 small functions extracting the different columns. Is it possible to factor it out to one function and pass the property as an argument? So sometimes I extractx.City but other times x.Country. I have tried using quotations however cannot splice it properly and maybe that is not the right approach.

推荐答案

关于引号拼接,这对我有用:

Regarding quotation splicing, this works for me:

open System.Linq

type record = { x:int; y:string }

let mkQuery q =
    query {
        for x in [{x=1;y="test"}].AsQueryable() do
        select ((%q) x)
    }

mkQuery <@ fun r -> r.x, r.y @>
|> Seq.iter (printfn "%A")

这篇关于参数LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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