简体   繁体   English

F#中的Seq和IQueryable

[英]Seq and IQueryable in F#

I am reading Web cloud and mobile solution with F# and I try to follow the web site the author is building. 我正在阅读使用F#的Web云和移动解决方案,并尝试关注作者正在构建的网站。 But I have some problem, I am unable to solve. 但是我有一些问题,我无法解决。 I understand the logic of what I am doing , but it looks like some piece of code is missing to make it work .I have read up to page 19. 我了解自己正在做的逻辑,但是似乎缺少一些代码才能使其正常工作。我已经阅读了第19页。

I have the following repository module : 我有以下存储库模块:

module Repository =
    let get (source : IQueryable<_>) queryFn=
        queryFn |> Seq.toList

    let getAll ()= 
        fun s -> query { for x in s  do
                         select x }

The idea is to use getAll in queryFn to get all items from source. 想法是在queryFn中使用getAll从源获取所有项。 But I have a cast problem between the two . 但是我在两者之间有一个问题。

Here is the controller that makes use of it: 这是使用它的控制器:

[<HandleError>]
type GuitarsController(context : IDisposable, ?repository ) =
    inherit Controller()


    let fromRepository =
        match repository with
        | Some v -> v
        | _ -> (context :?>  FsMvcAppEntities).guitars
                |> Repository.get



    new() = new GuitarsController(new FsMvcAppEntities())

    member this.Index() =
        getAll()
        |> fromRepository
        |> this.View

getAll() does not go well with |> fromRepository. getAll()与|> fromRepository配合不好。

The type ''b -> Linq.IQueryable<'c>' is not compatible with the type 'seq<'a>'. 类型``b-> Linq.IQueryable <'c>'与类型'seq <'a>'不兼容。

When looking at the type defined in the repository module I can see that queryFn is : 查看存储库模块中定义的类型时,我可以看到queryFn为:

val get : source:IQueryable<'a> -> queryFn:seq<'b> -> 'b list

and the getall gives 和getall给

 unit -> s:System.Linq.IQueryable<'a> -> System.Linq.IQueryable<'a>

Your Repository.get makes no use of source :D. 您的Repository.get不使用source :D。

Change it to: 更改为:

module Repository =
    let get (source : IQueryable<_>) queryFn =
        queryFn source |> Seq.toList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM