简体   繁体   中英

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. 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.

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. 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.

The type ''b -> Linq.IQueryable<'c>' is not compatible with the type 'seq<'a>'.

When looking at the type defined in the repository module I can see that queryFn is :

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

and the getall gives

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

Your Repository.get makes no use of source :D.

Change it to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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