title: Efficient way to take Entity Framework query results to Nested Dictionary?
body: <p>I'm hitting a loss at how to approach this problem. I am writing a function where I want the return type to be:</p>
  
  <pre><code>Dictionary(Of String, Dictionary(Of Integer, List(Of Article)))
  </code></pre>
  
  <p>I've basically got a list of articles that I wanted grouped by semester name, then by month value, then by the list of articles that fit into that.</p>
  
  <p>So far I have been able to do this:</p>
  
  <pre><code>Dim lectures As New Dictionary(Of Integer, List(Of Article))
          lectures = (From Articles In db.Articles _
             Order By Articles.StartDate.Value.Month Ascending _
             Where Articles.ArtType = ArticleConstants.lecturesTypeId _
             Group By Articles.StartDate.Value.Month _
             Into Group).AsEnumerable().Reverse.ToDictionary(Function(x) x.Month, Function(x) x.Group.ToList())
  </code></pre>
  
  <p>And this gets me the inner dictionary, however I am not sure where to go from here. Since I am only storing the  month as an integer, I can't use that to reliably determine the semester (as a semester is "Fall 2011"). I also can't grab the full date because then my grouping wouldn't work.</p>
  
  <p>I have a service to get the semester based on the Article's startDate that looks like this:</p>
  
  <pre><code>SemesterService.getSemester(Article.StartDate)
  </code></pre>
  
  <p>But again I am not sure how to apply this. I am thinking maybe I just start with all the articles in a given date range and then manually populate both the inner and outer dictionaries based on some logic which iterates through those results, but I have never done something this complex before and I am having trouble.</p>
  
  <p>So in the end I would have a result set that looks like:</p>
  
  <pre><code>Fall 2011 (outer dictionary key)
      1 (for January, inner dictionary key)
          Article 1
          Article 2 (list of articles, inner dictionary value)
          Article 3
  </code></pre>
  
  <p>Can anyone help me out?</p>
  
  <p><strong>EDIT:</strong> I am open to using a different return type, as long as it nests and groups data in a similar fashion.</p>
  
  <p>Semester is NOT an object stored in the database at all. It's just a static object I populate based on the date, so I cannot query by that. I just have my articles in the database, which has a startdate associated with it, and using that startdate I can calculate the semester it's in.</p>
  
  <p>Edit: Tried usr's solution below which is:</p>
  
  <pre><code>From Articles In db.Articles _
     Order By Articles.StartDate.Value.Month Ascending _
     Where Articles.ArtType = ArticleConstants.lecturesTypeId _
     Group New With { Articles, .Semester = SemesterService.getSemester(Articles.StartDate) } By Articles.StartDate.Value.Month _
     Into Group
  </code></pre>
  
  <p>Upon doing this I get an error: <code>LINQ to Entities does not recognize the method 'Semester getSemester(System.DateTime)' method, and this method cannot be translated into a store expression.</code></p>
  
  <p>It just seems to really hate grouping by something you calculate in the query but isn't in the original dataset.</p>