你如何执行多个查询?

时间:2014-05-28 06:07:47

标签: f#

我正在尝试根据我的函数中的参数执行多个后续查询。我无法弄清楚语法。

let returnVideoInfo catId isFeatured

    // main query
    let vidQuery = query{
        for row in db.Movies do
        where(row.IsFeatured = isFeatured)
        select row
    }    

    // I want to apply filters here based upon if arguments are null or not
    vidQuery
    |>
    (fun x -> 
    if catId <> null then
        Seq.filter (fun x -> x.CategoryId = catId) x
    else
    x)

这显然根本不起作用,所以我该如何解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

我不确定你在第一次通过阅读后的意思(可以提供数据集,格式和预期的返回结果),但是这会有效:

let returnVideoInfo catId (isFeatured : bool) = 
    let vidQuery = 
        match catId with
        | null -> ()
        | _ ->
            query{
                for row in db.Movies do
                where(row.IsFeatured = isFeatured && row.CatId = catId)
                select row
                exactlyOneOrDefault
            }
    vidQuery

let returnVideoInfo catId (isFeatured : bool) = 
    let vidQuery = 
        if catId <> null
            query{
                for row in db.Movies do
                where(row.IsFeatured = isFeatured && row.CatId = catId)
                select row
                exactlyOneOrDefault
        }
    vidQuery