I have created 2 separate lists using the page model like this:
@model IEnumerable<Batch>
@{
ViewBag.Title = "Batches";
bool FirstEveningBatch = true;
List<Batch> WeekDaysBatch = new List<Batch>(Model.ToList());
List<Batch> WeekEndBatch = new List<Batch>(Model.ToList());
}
Now I'm calling the extension method using the first list as in the below
@foreach (var item in WeekDaysBatch.Where(x => !x.IsWeekendBatch).AppendBatchNumber())
{
// ... some code ...
}
Here is the AppendBatchNumber()
extension method:
public static IEnumerable<Batch> AppendBatchNumber(this IEnumerable<Batch> batches)
{
bool IsFirstEveningBatch = true;
int batchnumber = 0;
Batch[] batchesArray = batches.OrderBy(x=> x.StartTime).ToArray();
for (int i = 0; i < batchesArray.Length; i++)
{
if (batchesArray[i].Name.StartsWith("Evening") && IsFirstEveningBatch)
{
batchnumber = 0;
IsFirstEveningBatch = false;
}
batchesArray[i].Name = string.Format("{0}-{1}", batchesArray[i].Name, ++batchnumber);
}
return batchesArray;
}
But the second list WeekEndBatch
is also getting altered. Is there any way how we can avoid the alteration of the second list?
Here is the Batch
class:
public class Batch
{
public int Id { get; set; }
public string Name { get; set; }
[NoBatchAfter9PM]
public TimeSpan StartTime { get; set; }
[NoBatchAfter9PM]
public TimeSpan EndTime { get; set; }
public string? Description { get; set; }
[Display(Name="Is it a Weekend batch?")]
public bool IsWeekendBatch { get; set; }
[Display(Name = "Is it a Women's batch?")]
public bool IsWomenBatch { get; set; }
public ICollection<Learner>? Learners { get; set; }
public Coach? Coach { get; set; }
}