使用F#3.0的动态SQL查询? [英] Dynamic SQL queries with F# 3.0?

查看:55
本文介绍了使用F#3.0的动态SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试使用FLINQ,但F#3.0 beta版已经过时了.

I have tried to use FLINQ but it is rather out of date with F# 3.0 beta.

有人可以给我一些有关如何在F#中创建动态SQL查询的提示吗?

Can someone give me some pointers on how to create dynamic SQL queries in F#?

推荐答案

在F#3.0中,查询会自动加引号,因此您不能使用引号拼接(<@ foo %bar @>语法)来构成查询.通过使用拼接来组成查询,您可以编写的大多数内容仍然可以通过以前的LINQ方式"来完成,方法是从先前的来源创建一个新查询并添加即过滤:

In F# 3.0, the query is quoted automatically and so you cannot use quotation splicing (the <@ foo %bar @> syntax) that makes composing queries possible. Most of the things that you could write by composing queries using splicing can still be done in the "usual LINQ way" by creating a new query from the previous source and adding i.e. filtering:

// Initial query that simply selects products
let q1 = 
  query { for p in ctx.Products do 
          select p }

// Create a new query that specifies only expensive products
let q2 = 
  query { for p in q1 do
          where (p.UnitPrice.Value > 100.0M) }

这样,您可以动态添加条件,动态指定投影(使用select)以及执行其他几个查询组合.但是,您没有像使用显式引号那样完全灵活地编写查询.我想这是F#3.0必须为类似于C#中存在的简单语法付出的代价.

This way, you can dynamically add conditions, dynamically specify projection (using select) and do a couple of other query compositions. However, you don't get the full flexibility of composing queries as with explicit quotations. I guess this is the price that F# 3.0 has to pay for a simpler syntax similar to what exists in C#.

原则上,您应该能够使用query.Select(等)运算符显式编写查询.这将使用显式引号编写,因此您应该能够使用拼接.但是,我不完全了解翻译的工作原理,因此无法给您提供工作样本.这样的事情应该可以工作(但是语法非常丑陋,所以最好只使用字符串或其他一些技巧):

In principle, you should be able to write query explicitly using the query.Select (etc.) operators. This would be written using explicit quotations and so you should be able to use splicing. However, I don't exactly know how the translation works, so I can't give you a working sample. Something like this should work (but the syntax is very ugly, so it is probably better to just use strings or some other techniques):

<@ query.Select(Linq.QuerySource<_, _>(ctx.Products), fun prod -> 
     // You could use splicing here, for example, if 'projection' is
     // a quotation that specifies the projection, you could write:
     //   %projection
     prod.ProductName) @>
|> query.Run

F#3.0中的查询基于IQueryable,因此可能可以使用与我为C#实现的那个.但是,我想某些细节可能会有所不同,因此我不希望这会立即生效.该想法的最佳实现是在 LINQKit 中,但我认为它不能直接起作用在F#中.

The queries in F# 3.0 are based on IQueryable, so it might be possible to use the same trick as the one that I implemented for C#. However, I guess that some details would be different, so I wouldn't expect that to work straight away. The best implementation of that idea is in LINQKit, but I think it won't directly work in F#.

因此,总的来说,我认为唯一可行的情况是第一个示例-您只需通过编写多个查询就可以对查询应用其他查询运算符.

So, in general, I think the only case that works well is the first example - where you just apply additional query operators to the query by writing multiple queries.

这篇关于使用F#3.0的动态SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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