Doubt in consultation with Entityframework

Asked

Viewed 47 times

0

how do I consult with EntityFramework?

with lambda or linq

SELECT t0051_id_medicamento, t0100_lote, SUM(t0100_qtde) FROM t0100_historico 
GROUP BY  t0051_id_medicamento, t0100_lote;
  • Speak the class name of your t0100_historic table if you can help.

  • the class name is Historico

  • Ok, try this: var db = new Dbcontext(); var result = db.Dbset<Historico>(). Tolist(). Groupby( new { p.Id, p.Lot }). Select(s => new Historico { Id = s.First().Id, Lot = s.First().Lot, Quantity = s.Sum(sum => sum.Quantity) }). Tolist();

  • @Wilsonsantos If there are many records, its expression will not be legal, because it first brings all the records of history - which will probably be MANY - to then make the groupings and summaries.

1 answer

2

Assuming that your table t0100_historico is represented by the entity Historico, then your lambda expression will look something like this:

db.DbSet<Historico>()
    .GroupBy(new { p.Id, p.Lote })
    .Select(s => new { s.First().Id, Lote, Total = s.Sum(sum => sum.Quantidade)})
    .ToList();

Browser other questions tagged

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