Concatenating F # Queries

I use the data provider SqlDataConnectionin F # for the transfer of certain lines of this migration is to make the connection between the three tables like this, think of it as a succession of tables A, B, Cwhere Band Care inherited from A, so I need to get (in Linq -like):

        Bs.Join(As, b.PK, a.FK).Select(new {...})
.Concat(Cs.Join(As, c.PK, a.FK).Select(new {...})

In F#, the closest I got to this:

 let result = seq {
     yield! query { ... }
     yield! query { ... }
 }

but I was told that this will produce 2 SQL queries and the overall result will be included. The question is: is there a way to do this "concatenation" as an expression of calculation querywithout using seq, so that everything happens in a single SQL query?

+3
1

, SQL UNION UNION ALL.

-, . , dbContext, , .

#r "System.Data.dll"
#r "System.Data.Linq.dll"
#r "FSharp.Data.TypeProviders.dll"

open System
open System.Linq
open Microsoft.FSharp.Data.TypeProviders

type sql = SqlDataConnection<connStr>

let createDbContext() = 
    let dbContext = sql.GetDataContext()
    // add logging to console
    dbContext.DataContext.Log <- System.Console.Out 
    dbContext 

let db = createDbContext()
let products = db.Product

let q1 = query { for x in products do select x }
let q2 = query { for y in products do select y }

UNION UNION

let qUnion = q1.Union(q2)
qUnion.ToList() |> Seq.toList

:

SELECT [t2].[Id], [t2].[Name]
FROM (
    SELECT [t0].[Id], [t0].[Name]
    FROM [dbo].[Product] AS [t0]
    UNION
    SELECT [t1].[Id], [t1].[Name]
    FROM [dbo].[Product] AS [t1]
    ) AS [t2]

Concat UNION ALL

let qConcat = q1.Concat(q2)
qConcat.ToList() |> Seq.toList

:

SELECT [t2].[Id], [t2].[Name]
FROM (
    SELECT [t0].[Id], [t0].[Name]
    FROM [dbo].[Product] AS [t0]
    UNION ALL
    SELECT [t1].[Id], [t1].[Name]
    FROM [dbo].[Product] AS [t1]
    ) AS [t2]

query, AFAIK.

+4

Source: https://habr.com/ru/post/1612291/


All Articles