F#Expr的匹配模式<;int>;

F#Expr的匹配模式<;int>;,f#,pattern-matching,f#-3.0,quotations,F#,Pattern Matching,F# 3.0,Quotations,我试图找到匹配的正确模式,并使用以下代码运行Expr: open System.Linq open Microsoft.FSharp.Quotations open Microsoft.FSharp.Quotations.Patterns let runSelectQuery (q:Expr<IQueryable<'T>>) = match q with | Application(Lambda(builder, Call(Some builder2

我试图找到匹配的正确模式,并使用以下代码运行
Expr

open System.Linq

open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns

let runSelectQuery (q:Expr<IQueryable<'T>>) = 
    match q with
    | Application(Lambda(builder, Call(Some builder2, miRun, [Quote body])), queryObj) ->
        query.Run(Expr.Cast<Microsoft.FSharp.Linq.QuerySource<'T, IQueryable>>(body))
    | _ -> failwith "Cannot run this query %s" (q.ToString())

let runCountQuery (q:Expr<int>) = 
    match q with
    | Application(Lambda(builder, Call(None, miRun, [builder2, Quote body])), queryObj) ->
        query.Run(Expr.Cast<int>(body))
    | _ -> failwith "Cannot run this query %s" (q.ToString())

let countQuery source filter =
    let filter = match filter with | Some filter -> filter | _ -> <@ fun _ -> true @>
    <@ query { for item in source do
               where ((%filter) item)
               count } @>
opensystem.Linq
打开Microsoft.FSharp.quotes
打开Microsoft.FSharp.quotes.Patterns
让我们运行selectquery(q:Expr>(body))
|->failwith“无法运行此查询%s”(q.ToString())
让runCountQuery(q:Expr)=
匹配q
|应用程序(Lambda(builder,Call(None,miRun,[builder2,Quote body])),queryObj)->
query.Run(Expr.Cast(body))
|->failwith“无法运行此查询%s”(q.ToString())
让countQuery源过滤器=
让筛选器=将筛选器与|某些筛选器->筛选器| |->true@>

runSelectQuery与找到的
Expr正确匹配!愚蠢的是,我第一次尝试使用逗号分隔的模式(如C#中的列表分隔符)匹配数组模式,这显然在F#中不起作用,因为它不是一个列表,而是一个元组,因此不是Rex

要重新匹配整型结果或任何“T”结果,请执行以下操作:

let runQueryToQueryable (q:Expr<IQueryable<'T>>) = 
    match q with
    | Application(Lambda(builder, Call(Some builder2, miRun, [Quote body])), queryObj) ->
        query.Run(Expr.Cast<Microsoft.FSharp.Linq.QuerySource<'T, IQueryable>>(body))
    | _ -> failwith "Cannot run this query %s" (q.ToString())

let runQueryToType (q:Expr<'T>) = 
    match q with
    | Application(Lambda(builder, Call(None, miRun, [builder2; Quote body])), queryObj) ->
           query.Run(Expr.Cast<'T>(body))
    | _ -> failwith "Cannot run this query %s" (q.ToString())
让runQueryToQueryable(q:Expr>(body))
|->failwith“无法运行此查询%s”(q.ToString())
让runQueryToType(q:Expr(body))
|->failwith“无法运行此查询%s”(q.ToString())

工作起来很有魅力。

这只是一个打字错误:)