Assuming your Linq2Sql implementation has the same relationships in it as the database (which, if you drag it into the constructor, they definitely do), thatβs how I could proceed.
I would create a strongly typed partial view of type Agent that will represent each section (Agency, in your case), name it AgencyReportSection.ascx. This control will be taken by the agency, iterating through its business units, which, in turn, are repeated through their customers.
Wherever you collect your data, do the following:
DataContext context = new DataContext(); DataLoadOptions options = new DataLoadOptions(); options.LoadWith<Agency>(a => a.BusinessUnit); options.LoadWith<BusinessUnit>(b => b.Client); context.LoadOptions = options;
What this will give you is that when the agency receives the context, it will follow certain relationships and give you these objects. So you get:
Agency a = context.Agency.FirstOrDefault(); IEnumerable<BusinessUnit> units = a.BusinessUnits; IEnumerable<Client> clients = units.Clients;
Your view might do something like:
<% foreach(var agency in agencies)%{> <% Html.RenderPartial("AgencyReportSection"); %> <%}%>
The reason you perform the function of loading data is to avoid lazy loading in the view so that the model collects all the necessary data.
I hope I understood your question correctly ...
source share