Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Note:</strong></p> <p>Since it's in EF v1, we will <strong>NOT</strong> have <strong>PersonLocation</strong> and <strong>PersonRole</strong> generated as entities like what LINQ2SQL does (The answer above domonstrating LINQ2SQL scenario, which doesn't apply to the question.)</p> <p><strong>Solution 1:</strong></p> <pre><code>Persons.Include("Role").Include("Location") // Include to load Role and Location .Where(p =&gt; p.Role.Any(r =&gt; r.description == "Student") &amp;&amp; p.Location.Any(l =&gt; l.description == "Amsterdam")).ToList(); </code></pre> <p>This looks nice and straightforward, but this generates ugly SQL script and its performance is ok.</p> <p><strong>Solution 2:</strong></p> <p>Here are breakdowns.</p> <pre><code> // Find out all persons in the role // Return IQuerable&lt;Person&gt; var students = Roles.Where(r =&gt; r.description == "Student") .SelectMany(r =&gt; r.Person); // Find out all persons in the location // Return IQuerable&lt;Person&gt; var personsInAmsterdam = Locations.Where(l=&gt; l.description == "Amsterdam") .SelectMany(l=&gt;l.Person); // Find out the intersection that gives us students in Admsterdam. // Return List&lt;Person&gt; var AdmsterdamStudents = students.Intersect(personsInAmsterdam).ToList(); </code></pre> <p>Combine three steps above into one:</p> <pre><code> //Return List&lt;Person&gt; var AdmsterdamStudents = Roles.Where(r =&gt; r.description == "Student") .SelectMany(r =&gt; r.Person) .Intersect ( Locations .Where(l=&gt; l.description == "Amsterdam") .SelectMany(l=&gt;l.Person) ).ToList(); </code></pre> <p>It's sort of verbose. But this generates clean SQL query and performs well.</p>
    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. 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