参数 LINQ 查询 [英] Parametric LINQ query

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

问题描述

这是另一种在F#中访问动态对象我正在使用let y = x.Where(fun x -> x.City ="London").Select("new(City,Zip)") 参数化查询并提取必要的项目.这些将对应于 SQL 查询中的列,并由数据上下文的属性表示.这是我想作为参数传入的部分.

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天全站免登陆