FLinq SQL“IN Clause”

时间:2010-11-05 20:19:33

标签: f#

我正在尝试通过f#linq创造一个“IN CLause” - 但似乎无法得到它。我尝试了两种选择:

作为序列表达式的一部分迭代天数列表:

let getHistoricalQuotes (securityId:int) (days:string seq) =
    let results = 
        Query.query <@ seq { 
            for day in days do
                for sq in db.SecurityQuote do
                    if sq.SecurityId =?! securityId && sq.Day = day then
                        yield sq ;
        } @> 

和List.exists子句:

let getHistoricalQuotes (securityId:int) (days:string list) =
    let results = 
        Query.query <@ seq { 
            for sq in db.SecurityQuote do
                if sq.SecurityId =?! securityId && List.exists (fun d -> d = sq.Day) days then
                    yield sq ;
        } @> 

    results

两人都在给我:

  

base {System.SystemException} =   {“方法   'Microsoft.FSharp.Core.FSharpFunc 2[System.String,System.Boolean] ToFSharpFunc[String,Boolean](System.Converter 2 [System.String,System.Boolean])'   没有受支持的SQL翻译。“}

一如既往,感谢您的帮助......


改写为:

let getHistoricalQuotes (securityId:int) (days:string list) =
    let results = 
        Query.query <@ 
            Query.join
                db.SecurityQuote 
                days
                (fun sq -> if(sq.SecurityId =?! securityId) then sq.Day else "") 
                (fun d -> d) 
                (fun sq d -> sq)  @> 

    results

并得到了这个例外:

  

System.Exception未处理
  消息=以下构造是   用于查询但不被识别   F#-to-LINQ查询转换器:值   ([“2010/01/04”;“2010/02/01”;   “2010/03/01”; “2010/04/01”;   “2010/05/03”; “2010/06/01”;   “2010/07/01”; “2010/08/02”;   “2010/09/01”; “2010/10/01”;   “2010/11/01”; “2010/12/01”;   “2010/01/29”; “2010/02/26”;   “2010/03/31”; “2010/04/30”;   “2010/05/31”; “2010/06/30”;   “2010/07/30”; “2010/08/31”;   “2010/09/30”; “2010/10/29”;   “2010/11/30”; “2010/12/31”])这是   不是有效的查询表达式。校验   允许查询的规范   并考虑移动一些查询   超出报价

并删除引号并没有导致错误 - 但它产生了一个非常不正确的查询(来自历史引用的selext *)


现在作弊,直到我可以回来修复问题 - 但至少我可以保持签名相同,但我会捶打数据库。

let getHistoricalQuotes (securityId:int) (days:string list) =
    let getQuote (day) =  
        Query.query <@ seq { 
            for sq in db.SecurityQuote do
                if sq.SecurityId =?! securityId && sq.Day = day then
                    yield sq ;
        } @> |> Seq.head

    List.map (fun day -> getQuote day) days

Per Will下面我尝试了这个

let getHistoricalQuotes (securityId:int) (days:string list) =
    let results =  
        Query.query <@ seq { 
            for sq in db.SecurityQuote do
                if sq.SecurityId =?! securityId && days.Contains(sq.Day) then
                    yield sq ;
        } @> 
    results

但它没有编译

  

错误1字段,构造函数或   成员'包含'未定义


最后 - (谢谢你):

let getHistoricalQuotes (securityId:int) (days:string array) =
    let results =  
        Query.query <@ seq { 
            for sq in db.SecurityQuote do
                if sq.SecurityId =?! securityId && days.Contains(sq.Day) then
                    yield sq ;
        } @> 
    results

是的 - 您需要打开System.Linq - 这些扩展方法,还是部分弹性 - 它们必须是,

1 个答案:

答案 0 :(得分:2)

它已经存在于Linq中,但与您使用的相反:

days.Contains(day)