In SQL I can write
SELECT a.*, b.*
FROM master_table a
LEFT JOIN detail_table b ON (b.a_id = a.id)
ORDER BY a.id, b.order_field
Is it possible to do the same with EF4 ?
I cannot understand how to specify order by clause.
So far I tried
List<master_table> l = context.master_table.Include("detail_table").
OrderBy(x=>x.id).
ThenBy( //here is the problem, y=>y.detail_table.order_filed doesn't compile,
//y=>y.detail_tables.OrderBy(z=>z.order_field) - throws a run-time exception
).
ToList();
Thanks.
The syntax in LINQ to Entities can be similar to your SQL query:
Edit:
With the clarification from your comment – The problem is that in the SQL query you have pairs of items that you are joining (a,b) – while in the Linq to Entities query you are trying to do you want a secondary order by a navigation property.
The difference is that there’s a one to many relationship between the
master_tableentries and thedetail_tableentries in this context, you have a grouping bymaster_tableentry already – given that it doesn’t make sense (to the compiler or in general) to be able to express that sort order on thedetail_tablelevel.I would just enforce it when you enumerate the results – the
master_tableentries are already in the right order, just return the details usingfoo.detail_tables.OrderBy(x=>x.order_field).