Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ/EF: conceptual/mechanics of doing multiple joins that return both an entity and a computed value
    primarykey
    data
    text
    <p>Im hoping someone can help me with a brain block I'm having about how to do multiple joins with EF that returns an entity, where one of the fields is calculated by a query. Ive contrived a (pretty much useless) example in the hopes somebody can help me understand how this should be done.</p> <p>Id like to return a list of ISPs entities from a DBContext with the "TotalUsed" property filled in by the LINQ query. Ive successfully done the joins (trivial), and have played with grouping and sum, but cant seem to get it quite right.</p> <p>The general idea to this contrived example is that NICs are connected to one and only one router, and multiple routers are connected to a single ISP each. </p> <p>How to write a LINQ query that sums the bandwidth needed for each ISP, along with the other properties in that ISP?</p> <p>Expected output would be a list of ISP that might look like {{1, "foo", 52}, {2, "bar", 345}, {3, "foobar", 621}}, where the 3rd property is the summation of the BandwidthNeeded property on all NICs transitively related to ISP.</p> <p>The classes:</p> <pre><code>public class ISP { public int ISPid {get; set;} public int ISPName {get; set;} public int TotalUsed {get; set;} // not mapped to DB -&gt; should populate via LINQ } public class Router { public int RouterId {get; set;} public string RouterName {get; set;} public int ISPId {get; set;} // foreign key for ISP } public class Nic { public int NicId { get; set; } public string NicLocation { get; set; } public int BandwidthUsed { get; set; } public int RouterId {get; set; } // foreign key for router } </code></pre>
    singulars
    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. This table or related slice is empty.
    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