title: How to get count of Dictionary Value Collection in C#
body: <p>I'm trying to get the count of a dictionary's value collection, where a field of the collection item, matches the key in the collection, as well as the type.</p>
  
  <p>So my collection looks like this, note the object is initialized, this is just pseudo code:</p>
  
  <pre><code>var results = Dictionary&lt;string, List&lt;CourseEnrollmentsByStudentSQLResult&gt;();
  </code></pre>
  
  <p>Now the <strong>CourseEnrollmentsByStudentSQLResult</strong> is a data transfer object, that maps to the rows returned from my query.</p>
  
  <pre><code>public class CourseEnrollmentsByStudentSQLResult
  {
      public int StudentID { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string College { get; set; }
      public int CollegeId { get; set; }
      public string Prefix { get; set; }
      public string CourseNumber { get; set; }
      public string Course { get; set; }
      public int CourseId { get; set; } 
      public string Program { get; set; }
      public string Term { get; set; }
      public string Status { get; set; }
      public List&lt;StudentCoursesSQLResult&gt; Courses { get; set; }
  }
  </code></pre>
  
  <p>You will notice that my <strong>CourseEnrollmentsByStudentSQLResult</strong> class also has a collection of <strong>StudentCoursesSQLResult</strong> objects. This is the list I'm trying to get a count of, based on two conditions.</p>
  
  <p>First that the course, which is the string KEY in the dictionary, matches the course of the <strong>StudentCoursesSQLResult</strong> and then I need to filter on the <strong>StudentCoursesSQLResult</strong>.Type.</p>
  
  <p>Here is the <strong>StudentCoursesSQLResult</strong> class:</p>
  
  <pre><code>public class StudentCoursesSQLResult
  {
      public int StudentID { get; set; }
      public int Position { get; set; }
      public string Prefix { get; set; }
      public string CourseNumber { get; set; }
      public string Course { get; set; }
      public string Type { get; set; }
  }
  </code></pre>
  
  <p>Now to get the count I'm looping over my dictionary and outputting headers for each key, this is where I'm trying to output the count for the dictionary's value collection.</p>
  
  <pre><code>    foreach(var result in results) {
         &lt;span class="label label-default label-fat"&gt;
            @result.Value.SelectMany(r =&gt; r.Courses).Count(c =&gt; c.Course == result.Key &amp;&amp; c.Type == "required")
         &lt;/span&gt;
      }
  </code></pre>
  
  <p>But for some reason it's returning zero, when I know it shouldn't be. </p>
  
  <p>Can someone please point me in the right direction? Everything I've found so far online has said to use SelectMany, but I'm not having any luck.</p>
  
  <p>Here is a screenshot of the debug output for a single course for the dictionary's value collection, where the Type is "required".</p>
  
  <p><img src="https://i.stack.imgur.com/K09Ek.png" alt="enter image description here"></p>