0

I have the following query

SELECT sdb.student_due_by_month_id, sdb.due_month_year, sum(sdb.due_total_month) FROM user_student ust
INNER JOIN student std ON ust.student_id = std.student_id
INNER JOIN student_due_by_month sdb ON std.student_id = sdb.student_id
WHERE ust.user_id = 2
GROUP BY sdb.student_due_by_month_id, sdb.student_due_by_month_id, sdb.due_month_year

I need to convert it to linq or a lambda expression. I'm trying to turn him into a linq and this is what I have so far:

 var user_due_month = await (from ust in _context.Users_students
                                    join std in _context.Students on ust.student_id equals std.student_id
                                    join sdb in _context.Students_dues_by_months on std.student_id equals sdb.student_id
                                    where ust.user_id == user_id
                                    group sdb by new { sdb.student_due_by_month_id, sdb.due_month_year } into g //from here i have yet to complete the linq query, help?

I don't know what would be missing to complete my linq query correctly, and also be able to pass it to a viewmodel.

Thanks a lot!!

  • 1
    I'm not sure what you are asking. Are you asking for someone to fill in your ellipsis (the three dots `...`). Your `from` code that you show will return an `IQueryable`. So you can't `await` it (it's not a Task). You'll need to await`ToListAsync` or something similar to get the results from the query. – Flydog57 Mar 06 '20 at 21:53
  • What I need is a functional linq query based in my sql query – Carlos Adrian Mar 06 '20 at 22:00
  • Start with a simple query to get the pattern. Then add more complexity. – Gert Arnold Mar 06 '20 at 22:03
  • This is the linq I've been carrying so far: `from ust in _context.Users_students join std in _context.Students on ust.student_id equals std.student_id join sdb in _context.Students_dues_by_months on std.student_id equals sdb.student_id where ust.user_id == user_id group sdb by new { sdb.student_due_by_month_id, sdb.due_month_year } into g` – Carlos Adrian Mar 06 '20 at 22:08
  • Perhaps my [SQL to Linq Recipe](https://stackoverflow.com/questions/49245160/sql-to-linq-with-multiple-join-count-and-left-join/49245786#49245786) might help you. – NetMage Mar 06 '20 at 22:33
  • Seems like you just need to add `select new { g.Key.student_due_by_month_id, g.Key.due_month_year, due_total = g.Sum(sdb => sdb.due_total_month) }` – NetMage Mar 06 '20 at 22:36

0 Answers0