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

Asked

Viewed 831 times

2

I’m developing a system in ASP.NET MVC which uses as standard the Entity Framework for data access, but I am in need of a select on another select, and I don’t know how to do this with Entity. The code SQL which I 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 anyone can help me, I’d be grateful!

  • Could put the entities?

  • Could you explain the purpose of this consultation? It will facilitate the solution of your problem (because there may be other better ways to solve it).

  • The query is necessary to list all requests along with the last scheduled by the same.

  • @Lucaskauer would be a simple & #Xa;Orders 1->n Schedules

1 answer

2


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

That is to say:

var pedidos = db.Agendamentos   
                .Include(a => a.Pedidos)
                .Where(a => a.Realizado == 0)
                .OrderByDescending(a => a.Data)
                .FirstOrDefault()
                .SelectMany(a => a.Pedidos)
                .ToList();
  • The field Accomplished is on the table of scheduling, who holds the key pedidosID (FK) and also a Date which shows when it was scheduled. What I need to do is List all orders including the latest schedule accomplished and that was not paid.

  • Did you implement my answer? If so, what was the result? In addition, you edited your question by asking what the purpose of the selection is, according to this comment you made in my reply?

  • 1

    Thinking about what you did I succeeded with the following code: var pedidos = Db.Agendamentos.Where(m => m.Data < DateTime.Now).Where(m => m.Realizado == false).ToList();&#xA;Db.Agendamentos.Include("Pedido").ToList(); Thank you!

Browser other questions tagged

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