Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One of the great things about Linq is that you don't HAVE to write this all as one monolithic query because it won't actually execute until you enumerate the results anyway. You <em>could</em> write a single query, but you don't <em>have</em> to. Instead, you can write this as multiple, separate queries, increasing the readability, and clarifying your intent.</p> <pre><code>var sportIds = Sports .Where(s =&gt; s.Name == "Hockey" || s.Name == "Soccer") .Select(s =&gt; s.Id); var people = Person.Where(p =&gt; SportsPerPerson .Count(spp =&gt; (spp.PersonId == p.Id) &amp;&amp; sportIds.Contains(spp.SportId)) == 2); </code></pre> <p>First, we get the collection of sport Ids we're interested in. Then, we find all the people with two sports in the first list. Although it's expressed as multiple queries, Linq will compress it all into one operation for us when we finally enumerate the results.</p> <p>EDIT: Here is a complete test class illustrating the query:</p> <pre><code>using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace L2STest { public class Sport { public int Id { get; set; } public string Name { get; set; } } public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } public class SportPerPerson { public int Id { get; set; } public int PersonId { get; set; } public int SportId { get; set; } } [TestClass] public class SportsTest { private List&lt;Person&gt; persons; private List&lt;Sport&gt; sports; private List&lt;SportPerPerson&gt; sportsPerPerson; [TestInitialize] public void MyTestInitialize() { persons = new List&lt;Person&gt; { new Person {Id = 1, FirstName = "John", LastName = "Doe"}, new Person {Id = 2, FirstName = "Peter", LastName = "Svendson"}, new Person {Id = 3, FirstName = "Ola", LastName = "Hansen"}, new Person {Id = 4, FirstName = "Marv", LastName = "Petterson"}, }; sports = new List&lt;Sport&gt; { new Sport {Id = 1, Name = "Tennis"}, new Sport {Id = 2, Name = "Soccer"}, new Sport {Id = 3, Name = "Hockey"}, }; sportsPerPerson = new List&lt;SportPerPerson&gt; { new SportPerPerson {Id = 1, PersonId = 1, SportId = 1}, new SportPerPerson {Id = 2, PersonId = 1, SportId = 3}, new SportPerPerson {Id = 3, PersonId = 2, SportId = 2}, new SportPerPerson {Id = 4, PersonId = 2, SportId = 3}, new SportPerPerson {Id = 5, PersonId = 3, SportId = 2}, new SportPerPerson {Id = 6, PersonId = 3, SportId = 1}, new SportPerPerson {Id = 7, PersonId = 4, SportId = 2}, new SportPerPerson {Id = 8, PersonId = 4, SportId = 3}, }; } [TestMethod] public void QueryTest() { var sportIds = sports .Where(s =&gt; s.Name == "Hockey" || s.Name == "Soccer") .Select(s =&gt; s.Id); var people = persons.Where(p =&gt; sportsPerPerson .Count(spp =&gt; (spp.PersonId == p.Id) &amp;&amp; sportIds.Contains(spp.SportId)) == 2); Assert.AreEqual(2, people.Count()); Assert.AreEqual("Peter", people.First().FirstName); Assert.AreEqual("Marv", people.Last().FirstName); } } } </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. VO
      singulars
      1. This table or related slice is empty.
    2. 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