Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a solution which requires only a single roundtrip:</p> <pre><code>var orders = Context.Orders .Select(o =&gt; new { Order = o, SubOrderBases = o.SubOrderBases.Where(s =&gt; !(s is MyOrder)), MyOrdersWithCustomers = o.SubOrderBases.OfType&lt;MyOrder&gt;() .Select(m =&gt; new { MyOrder = m, Customers = m.Customers }) }) .ToList() // &lt;- query is executed here, the rest happens in memory .Select(a =&gt; { a.Order.SubOrderBases = new List&lt;SubOrderBase&gt;( a.SubOrderBases.Concat( a.MyOrdersWithCustomers.Select(m =&gt; { m.MyOrder.Customers = m.Customers; return m.MyOrder; }))); return a.Order; }) .ToList(); </code></pre> <p>It is basically a projection into an anonymous type collection. Afterwards the query result is transformed into entities and navigation properties in memory. (It also works with disabled tracking.)</p> <p>If you don't need entities you can omit the whole part after the first <code>ToList()</code> and work directly with the result in the anonymous objects.</p> <p>If you must modify this object graph and need change tracking, I am not sure if this approach is safe because the navigation properties are not completely set when the data are loaded - for example <code>MyOrder.Customers</code> is <code>null</code> after the projection and then setting relationship properties in memory could be detected as a modification which it isn't and cause trouble when you call <code>SaveChanges</code>.</p> <p>Projections are made for readonly scenarios, not for modifications. If you need change tracking the probably safer way is to load full entities in multiple roundtrips as there is no way to use <code>Include</code> in a single roundtrip to load the whole object graph in your situation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload