I have this following code which will return all the current semesters. How do I identify if there is "Summer" semester in the collection and replace it with "Summer I" dynamically?
return activeSemester.Select(c => new ActiveSemester
{
id = c.SemesterId,
name = c.Name, // Here I want to check if it is Summer
}).ToList();
Update: Summer semesters hold 3 Summer names but the dates are different. I just want to name it in order. Also each one has unique SemesterId.
So you can accurately account for your 3 different summer sessions, I think a conditional approach would make more sense. If you implement any kind of incrementing method and pass data to this method that is not sorted correctly, you could tag an ActiveSemster
as "Summer I"
when the dates or semester id match "Summer II"
You could either include the condition directly in a LINQ query or create a method that will identify the summer based on date or semster id
return activeSemester.Select(c => new ActiveSemester
{
id = c.SemesterId,
name = c.Name == "Summer" ? GetSummmer(c.StartDate, c.EndDate) : c.Name
}).ToList();
private string GetSummer(DateTime startDate, DateTime endDate)
{
if (startDate == summer1Start || endDate == summer1End)
return "Summer I";
if (startDate == summer2Start || endDate == summer2End)
return "Summer II";
if (startDate == summer3Start || endDate == summer3End)
return "Summer III";
return "Unknown Summer";
}
private string GetSummer(Integer semesterId)
{
if (semesterId == summer1Id)
return "Summer I";
if (semesterId == summer2Id)
return "Summer II";
if (semesterId == summer3Id)
return "Summer III";
return "Unknown Summer";
}
return activeSemester.Select(c => new ActiveSemester
{
id = c.SemesterId,
name = c.Name == "Summer" ? "Summer I" : c.Name
}).ToList();
SemesterId
? The latter would be simple - psubsee2003 2012-04-05 19:39
return activeSemester.Select(c => new ActiveSemester
{
id = c.SemesterId,
name = c.Name.Replace("Summer", "Summer I") // Here I want to check if it is Summer
}).ToList();
While I'm at it: why are you calling .ToList()? That's almost always wrong, and it's generally better to go for IEnumerable<T> rather than IList<T> or List<T>.
IEnumerable
in certain situations, I'm just not entirely convinced that preferring IEnumerable
is necessarily the right choice in (almost) every situation - Stuart Golodetz 2012-04-06 12:27
For a generalized solution for assignment of any semester number for any term you could do the following by sorting on semester start date. This way you don't have to rely on equaliting on dates or ids. Not as elegant or performing perhaps as psubsee2003 answer. ConvertToRomanNumeral
can be implemented by searching SO.
public IEnumerable<ActiveSemester> GetActiveSemesters()
{
int summerSemesterNumber = 1;
int winterSemesterNumber = 1;
foreach (ActiveSemester activeSemester in _activeSemesters.OrderBy(c => c.StartDate))
{
if (activeSemester.Name == "Summer")
{
yield return new ActiveSemester(activeSemester)
{
Name = string.Format("{0} {1}", activeSemester.Name,
ConvertToRomanNumeral(summerSemesterNumber++))
};
}
else if (activeSemester.Name == "Winter")
{
yield return new ActiveSemester(activeSemester)
{
Name = string.Format("{0} {1}", activeSemester.Name,
ConvertToRomanNumeral(winterSemesterNumber++))
};
}
else
{
yield return new ActiveSemester(activeSemester);
}
}
}