Dzoukr / Dapper.FSharp

Lightweight F# extension for StackOverflow Dapper with support for MSSQL, MySQL, PostgreSQL, and SQLite
MIT License
365 stars 35 forks source link

How to do use IN operator in LINQ queries? #35

Closed tforkmann closed 3 years ago

tforkmann commented 3 years ago

I'm trying to convert SqlProvider queries to Dapper.FSharp queries.

In Sqlprovider you can use |=| as an IN operator in the where clause.

Would be nice if I could do something like that with Dapper.FSharp.

I tried this which obviously does not work:

let getIdsAndDescriptions (conn: IDbConnection)=
    task {
        let! ids =
            select {
            for e in enmsTabelle do
            join datapoints  in enmsSichtDatenpunkte on (e.Sakto = datapoints.Sakto)
            where (datapoints.Mandant = int16 999
                && datapoints.USER_KstEbene |=| [|"Contracting";"BGA";"KW"|])
            distinct
             }
            |> conn.SelectAsync<{| Saktobez: string ; Sakto : string ; USER_KstEbene : string |}>
        return ids |> Seq.toArray
    }

Any idea how to implement that?

JordanMarr commented 3 years ago

This should work.

let getIdsAndDescriptions (conn: IDbConnection)=
    task {
        let mandant = int16 999
        let! ids =
            select {
                for e in enmsTabelle do
                join datapoints  in enmsSichtDatenpunkte on (e.Sakto = datapoints.Sakto)
                where (
                    datapoints.Mandant = mandant &&
                    isIn datapoints.USER_KstEbene [ "Contracting";"BGA";"KW" ])
                distinct
             }
             |> conn.SelectAsync<{| Saktobez: string ; Sakto : string ; USER_KstEbene : string |}>
        return ids |> Seq.toArray
    }
JordanMarr commented 3 years ago

I think I missed your intent. To implement that you would need to stub out the new operator, and then check for it alongside the isIn / isNotIn MethodCall handler. That's just a guess - not sure if it would be a straightforward add.

tforkmann commented 3 years ago

Thanks @JordanMarr!

I think the isIn operator is totally fine. Didn't know those operators work for then LINQ queries stlye as well.

Closing!