Answer the question
In order to leave comments, you need to log in
How to properly build linq with subquery?
Good afternoon!
There is a table of CargoOrder orders. There is a history table of operations HistoryEvent. There are many operations for one order.
How to make a selection, take an order and indicate its last operation, that is, join an operation with a maximum date? Whether a subquery, or let, how is it cooked? I can write it in sql with a subquery, but I don’t understand how to transfer it to linq
Something like this:
var q = from co in db.CargoOrder
join he in db.HistoryEvent
on co.OrderNo equals he.OrderNo
select new
{
co.OrderNo,
co.State,
he.Event,
he.ServiceMCS
};
Answer the question
In order to leave comments, you need to log in
Are you looking for something like this?
from p in context.ParentTable
join c in context.ChildTable on p.ParentId equals c.ChildParentId into j1
from j2 in j1.DefaultIfEmpty()
group j2 by p.ParentId into grouped
select new { ParentId = grouped.Key, Count = grouped.Count(t=>t.ChildId != null) }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question