I've been working on an older project with framework is 3.5 as the target. There is a new user control that displays a list of data from a web service. I hit the web service (Soap/XML) and deserialize the data into a list of a custom model/class (this is in a helper class outside the user control).
Back in the user control code behind, I'm looping through the data and displaying it using a HtmlTextWriter
. Here is an example of some of the code (simplified):
var sb = new StringBuilder();
using (var writer = new HtmlTextWriter(new StringWriter(sb)))
{
foreach (
var month in
courses.SelectMany(x => x.Offerings)
.Where(x => !string.IsNullOrEmpty(x.StartDate))
.Select(x => DateTime.Parse(x.StartDate))
.Select(x => new DateTime(x.Year, x.Month, 1))
.Distinct()
.OrderBy(x => x.Year)
.ThenBy(x => x.Month)
.ToList())
{
//some display code using the writer
foreach (var offering in
courses.SelectMany(x => x.Offerings)
.Where(x => !string.IsNullOrEmpty(x.StartDate))
.Where(
x =>
DateTime.Parse(x.StartDate).Year == month.Year
&& DateTime.Parse(x.StartDate).Month == month.Month)
.OrderBy(x => DateTime.Parse(x.StartDate))
.ToList())
{
//more display code using the writer
}
}
}
thisisadiv.InnerHtml = sb.ToString();
The HTML markup is a design that was already in place somewhere else so I chose not to change it at all in the interest of time.
Is there a more efficient way of looping through the classes/data that I could benefit from? I'm currently not seeing any speed issues but want to make sure I'm using an adequate technique.