V
V
Victor P.2018-07-24 15:51:49
C++ / C#
Victor P., 2018-07-24 15:51:49

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
                        };

Only from HistoryEvent need one record with max date

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2018-07-24
@Jeer

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) }

https://stackoverflow.com/a/695510

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question