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)}
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.
select (customer, customer.City)
and yourqryfun
will accept tuple as a parameter Since all variety of customer properties dont have static type, you either use tuples for intermediate data flow between functions/transformations or define your own record type and map it in your query – Goulettelet qryfun (x, x.column) = query { for x in query2 do select (x.column)}
where x is the DB table (Northwind.ServiceTypes.Customer) and x.column is a Column (or field) in the Customer table, it could be x.City, x.Country, etc.... then I would callqryfun (Northwind.ServiceTypes.Customer, Northwind.ServiceTypes.Customer.City)
The 2nd argument is tricky because Northwind.ServiceTypes.Customer.City belongs to each record not to the table. If you take a look at the referenced question, I bult it from a string ("new(City, Country)") – Forepleasure