How do I change the name from the collection from following C# code?

Go To StackoverFlow.com

1

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.

2012-04-05 19:01
by HardCode


1

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";
}
2012-04-05 20:02
by psubsee2003
Thank you, Nice idea - HardCode 2012-04-05 20:17


5

return activeSemester.Select(c => new ActiveSemester
{
    id = c.SemesterId,
    name = c.Name == "Summer" ? "Summer I" : c.Name
}).ToList();
2012-04-05 19:02
by RedFilter
It works great, I also have three summer semesters! How do I increment the value. For instance Summer I, Summer II, Summer III? Thank you - HardCode 2012-04-05 19:05
So replace Summer I with Summer II? etc. Is that Roman Numerals - Luke Hutton 2012-04-05 19:08
Yes, Summer semesters hold 3 summer names but the dates are different. I just want to name it in order - HardCode 2012-04-05 19:10
@HardCode, can you distinguish between semesters by the SemesterId - RedFilter 2012-04-05 19:11
Yes it increment from the first one - HardCode 2012-04-05 19:12
Can I increment by Count - HardCode 2012-04-05 19:25
Do you have DateTime properties on ActiveSemester? Can you assign the semester number based on that - Luke Hutton 2012-04-05 19:30
Yes, it has Start date and End date. But I want to show I, II, III instead of date. Is it possible to increment a value in the collection? For instance count++ or something. - HardCode 2012-04-05 19:35
@HardCode Is there a reason why you prefer to use a count instead of a conditional check based on the SemesterId? The latter would be simple - psubsee2003 2012-04-05 19:39
@psubsee2003 Well, there is no preference. I just want to increment the summer values based on roman numerals - HardCode 2012-04-05 19:46


0

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>.

2012-04-05 19:03
by Joel Coehoorn
Why do you say it's almost always wrong out of curiosity? Do you mean wrong just in the context of returning from a method, or more generally - Stuart Golodetz 2012-04-05 20:12
It's wrong because it ties to you a more complicated API that you're not likely to use (eg, it makes it harder to change to an array or other collection type later), and it's wrong for performance reasons, because it forces you to materialize the list items right away, often at the expense of making unnecessary copies, rather than setting you up for streaming only one item at a time - Joel Coehoorn 2012-04-05 21:44
Ok, fair enough. I think I was trying to clarify the scope of "almost always wrong" really - I can imagine contexts where you'd want the sequence to be evaluated up-front (e.g. perhaps you're retrieving a small amount of information from a database to be used later, and don't want to leave the connection open in the meantime). I entirely agree with the reasons you give in the comment for preferring 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


0

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);
    }
  }
}
2012-04-05 21:02
by Luke Hutton
Ads