How to do a select from a select using the Entity Framework ado.net

2

I'm developing a system in ASP.NET MVC that defaults to but I'm in need of a select over another select , and I do not know how to do this with Entity . The SQL code you would like to use is the following:

select * from pedidos as p
                 inner join agendamentos as a
                 on a.pedidoID = p.pedidoID
                 where a.data = (select max(data) from (select a.data from pedidos as p
                 inner join agendamentos as a
                 on a.pedidoID = p.pedidoID
                 where a.realizado = 0) as dataagendamentos);

If someone can help me, I'll be grateful!

    
asked by anonymous 06.02.2017 / 20:59

1 answer

2

You should not use the Entity Framework as SQL because it is not SQL. The sentence needs to be rethought. You should start with the schedules including your requests, not the requests including the schedules.

That is:

var pedidos = db.Agendamentos   
                .Include(a => a.Pedidos)
                .Where(a => a.Realizado == 0)
                .OrderByDescending(a => a.Data)
                .FirstOrDefault()
                .SelectMany(a => a.Pedidos)
                .ToList();
    
06.02.2017 / 21:27