user3616544
user3616544

Reputation: 1073

Entity Framework Linq query unrelated child tables

I am using Entity Framework 6.1.3 to generated the entities and data model.

If I have two tables: Orders -> OrderDetails, with a relation between them (OrderId), then I can get all the orders and related OrderDetails with the following query

dbContext.Order().Include(a => a.OrderDetails);

But if I created a view (vOrder) for Orders, then there is no direct relation between vOrder and OrderDetails in the model, though I can link them together with joins on OrderId. How could I still get all the data from vOrder and related OrderDetails. The following query doesn't work unless I add all the navigation properties manually.

dbContext.vOrder().Include(a => a.OrderDetails);

Is there a simple LINQ query to accomplish the intended query?

Thanks for your help.

Upvotes: 0

Views: 945

Answers (2)

Wyatt Earp
Wyatt Earp

Reputation: 1823

Why not just include more columns in the view (or create another view that has all the required data, if you don't want to modify the first one)?

Upvotes: 0

Robert McKee
Robert McKee

Reputation: 21487

Do a manual join and return an anonymous object that contains both.

Something like:

dbContext.vOrder
  .GroupJoin(
    dbContext.OrderDetails,
    v=>v.orderid,
    od=>o.orderid,
    (v,od)=>new {v=v,od=od});

Of course, you could just add the appropriate naviation properties on to vOrder and do exactly what you said.

Upvotes: 2

Related Questions