I have an ASP.NET MVC application which I have being using with LINQ to SQL for a while now. I have got the hang of replicating most queries in LINQ but there is one that has had me stumped for several days now.
I am trying to select a list of "Progressions" where a condition is met, and I have a list of groups.
My ERD is as follows:
"Group" 1<->Many "Enrolments" Many<->1 "Students" 1<->Many "Progressions"
And the standard SQL would be (Except in the code I have a specific set of groups passed to the function):
SELECT dbo.[Group].GroupID, COUNT(*) AS returning
FROM dbo.[Group] INNER JOIN
dbo.Enrolment ON dbo.[Group].CourseID = dbo.Enrolment.GroupID INNER JOIN
dbo.Student ON dbo.Enrolment.StudentID = dbo.Student.StudentID INNER JOIN
dbo.Progression ON dbo.Student.StudentID = dbo.Progression.StudentID
WHERE (dbo.Progression.IsReturning = 0)
GROUP BY dbo.[Group].GroupID
Now for the Web App. The ASP view "Progression" gets passed the varibale "groups" which is a list of a few selected groups. I am currently using the follwing code, which is very slow (30 secs or more to load page)
<%foreach (var tg in Model)
{%>
<% notreturning = 0; %>
<%foreach (Enrolment e in tg.Enrolments)
{
notreturning = notreturning + e.Student.Progressions.Where(p => !p.IsReturning).Count();
}%>
<tr>
<td><% = notreturning %></td>
</tr>
<%
} %>
I am counting some other stuff too but for this example I'll stick to one. Now obviously this is quite slow because it has to do a foreach for groups, then for each enrolment in the group, so around 10 groups times 20 students in each. I deally I want to do something like the following which eliminates the second foreach:
<%foreach (var tg in Model)
{%>
<% notreturning = 0; %>
<%var test = tg.Enrolments.Where(e => e.Student.Progressions.Where(p => !p.IsReturning)).Count(); %>
<tr>
<td><% = notreturning %></td>
</tr>
<%
} %>
That code doesn't work as the nested where clause doesn't return a bool data type, but I hope it get accross what I'm trying to do here.
I'm not sure if I've explained this very well but if anyone has any ideas I would be very grateful, this has been bugging me for days!