Efficient way to take Entity Framework query results to Nested Dictionary?
I'm hitting a loss at how to approach this problem. I am writing a function where I want the return type to be:
Dictionary(Of String, Dictionary(Of Integer, List(Of Article)))
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.
So far I have been able to do this:
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())
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.
I have a service to get the semester based on the Article's startDate that looks like this:
SemesterService.getSemester(Article.StartDate)
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.
So in the end I would have a result set that looks like:
Fall 2011 (outer dictionary key)
1 (for January, inner dictionary key)
Article 1
Article 2 (list of articles, inner dictionary value)
Article 3
Can anyone help me out?
EDIT: I am open to using a different return type, as long as it nests and groups data in a similar fashion.
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.
Edit: Tried usr's solution below which is:
From Articles In db.Articles _
Order By Articles.StartDate.Value.Month Ascending _
Where Articles.ArtType = ArticleConstants.lecturesTypeId _
Group 开发者_JS百科New With { Articles, .Semester = SemesterService.getSemester(Articles.StartDate) } By Articles.StartDate.Value.Month _
Into Group
Upon doing this I get an error: LINQ to Entities does not recognize the method 'Semester getSemester(System.DateTime)' method, and this method cannot be translated into a store expression.
It just seems to really hate grouping by something you calculate in the query but isn't in the original dataset.
I will answer in pseudo-C#:
from s in semesters
group s by s.Name into g
select new {
SemName = g.Key,
MonthsItems = (from y in g group y by y.Month into g2 select new { Month = g2.Key, ... }
}
This is how you do a nested grouping. However! This will yield depressingly bad perf. Be sure do replace "semesters" with "semesters.AsEnumerable()" to cause the nested grouping to be evaluated on the client. SQL does not have the ability to return trees so this cannot be done on the server. Hope this helps, if unclear, comment and I will elaborate.
As an alternative, do this:
from s in semesters
group s by new { s.Name, s.Month }
That way you can execute fully on the server.
Edit: From the comments I can tell the problem is something different.
From Articles In db.Articles _
Order By Articles.StartDate.Value.Month Ascending _
Where Articles.ArtType = ArticleConstants.lecturesTypeId _
Group new { Articles, Semester = SemesterService.getSemester(Articles.StartDate) } By Articles.StartDate.Value.Month _
Into Group
Notice that the group by feature allows to to have two paramters not one. You can not only specify the grouping columns but also the values you want to have in each group. In this case I specified to group by month not only the articles but to also retain their semesters. Jope this helps.
精彩评论