Union All for Entity framework using Lambda

Asked

Viewed 125 times

1

I’m having problems in a query using lambda and Entity Framework in C#. When transforming a simple sql query into lambda works perfectly, as example below.

SQL query

    public DataSet ListarRecebidos()
    {
        ch.Conectar();
        string sql = "";
        sql = "SELECT * FROM SCEBD019 WHERE CLIMOVI>='"+VCLIMOVI+"' AND CLIPAGO <='"+VCLIPAGO+"'";
        SqlDataAdapter da = new SqlDataAdapter(sql, ch.cn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ch.Desconectar();
        return ds;
    }

Consulta Lambda

public object ListarContasRecebidas(DateTime dataMovi, DateTime dataPagamento) => context.sCEBD019s.Where(c => c.CLIMOVI >= dataMovi && c.CLIPAGO <= dataPagamento).Select(c => new { c.PRONOTA, c.CLIPAGO, c.CLIRAZA, c.CLIVPAG, c.PAGCODI, c.PAGNOME, c.CARNOME, c.CLIMOVI, c.CLIPROC, c.CLIVENC, c.CLIREFE, c.CLICHEQ, c.CLIBANC, c.CLIOBSE, c.FUNCODI, c.CLIDUPL}).AsNoTracking().ToList();

Now, a sql query using the UNION ALL I can’t do lambda at all. I’ve searched several forums, but I haven’t seen anyone using lambda, only LINQ.

If anyone has any tips or suggestions I really appreciate it, because I’ve wasted a lot of time on it. It doesn’t even have to be answer or code ready, only a north is enough.

Below is the sql query using the UNION ALL

public DataSet ListarTodos()
    {
        ch.Conectar();
        string sql = "";
        sql = "SELECT * FROM SCEBD018 where SCEBD018.CLICODI='"+VCLICODI+"' AND SCEBD018.CLIMOVI>='"+VCLIMOVI+"' " +
            "AND SCEBD018.CLIVENC<='"+VCLIVENC+"' UNION ALL (SELECT * FROM SCEBD019 WHERE " +
            "SCEBD019.CLICODI = '"+VCLICODI+"' AND SCEBD019.CLIMOVI >= '"+VCLIMOVI+"' AND " +
            "SCEBD019.CLIVENC <= '"+VCLIVENC+"')";
        SqlDataAdapter da = new SqlDataAdapter(sql, ch.cn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ch.Desconectar();
        return ds;
    }
  • in my view what you are looking for is the Concat ex: (select 1). Concat(select 2)

  • @Lucasmiranda, already tried to use the Concat, according to the following code snippet: public Object Listartodos(Datetime dataMovi, Datetime dataVenc, int codeClient) => context.sCEBD018s.Tolist(). Concat(context.sCEBD019s.Tolist());, but get the following error: CS1929 error "List<SCEBD018>" does not contain a definition for "Concat" and the best overload of the extension method "Queryable.Concat<SCEBD019>(Iqueryable<SCEBD019>, Ienumerable<SCEBD019>)" requires a receiver of type "Iqueryable<SCEBD019>"

  • You must have already solved, but the Concat comes before the Tolist() the last effective query at the base and brings the result

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.