I'm trying to figure out how to loop from one list to another.
var monthlySchedule = db.StudioSchedules.ToList();
List<CalenderColumns> ObjCal = new List<CalenderColumns>();
foreach (var item in monthlySchedule)
{
ObjCal = new List<CalenderColumns>()
{
new CalenderColumns
{
id = item.ID, name = item.Description,
startdate = item.Date.ToString(), enddate=item.Date.ToString(),
starttime =item.StartTime, endtime=item.EndTime,
color ="#99CCCC", url=""
}
};
}
Console.WriteLine(ObjCal);
First I tell the program to put monthlySchedule
into a list. Which contains 4 rows of data
Then I make a new list called ObjCal from a public class
public class CalenderColumns
{
public int id { get; set; }
public string name { get; set; }
public string startdate { get; set; }
public string enddate { get; set; }
public string starttime { get; set; }
public string endtime { get; set; }
public string color { get; set; }
public string url { get; set; }
}
Next, I write a foreach
loop of monthlySchedule
which contains the 4 rows to add it to ObjCal
The issue is the foreach loop is only getting the last row but I'm trying to get all 4 rows. Shown below
Goal: To have all 4 rows of data in ObjCal
CodePudding user response:
Why your current solution isn't working:
Instead of adding the items to your ObjCal
list you are currently creating a new list for each item (you are calling new List<CalenderColumns>()
inside the loop). This newly created list (which contains only one item as specified in the object initializer) is then stored in the ObjCal
variable. This is repeated for as many times as you have elements in monthlySchedule
leaving you finally with a list which only contains the last element of monthlySchedule
(resulting from the last iteration of the foreach
loop).
How you can fix it:
What you want to do is to add each item to the existing list (which you rightfully created before the foreach
loop). Items can be added to a list by calling the Add()
method and passing the item you want to add to it. As you want to add all elements to the ObjCal
the call to Add()
should be placed inside of the foreach
loop (so that it's repeated for all elements). One possible solution may therefore look like this:
foreach (var item in monthlySchedule)
{
CalenderColumns columns = new CalenderColumns
{
id = item.ID, name = item.Description,
startdate = item.Date.ToString(), enddate=item.Date.ToString(),
starttime =item.StartTime, endtime=item.EndTime,
color ="#99CCCC", url="" }
};
// don't create a new list but add the new entry to the existing
// list we created before entrering the loop
ObjCal.Add(columns);
}
or if you prefer LINQ:
var monthlySchedule = db.StudioSchedules.ToList();
List<CalenderColumns> ObjCal = monthlySchedule
.Select(item => new CalenderColumns
{
id = item.ID, name = item.Description,
startdate = item.Date.ToString(), enddate=item.Date.ToString(),
starttime =item.StartTime, endtime=item.EndTime,
color ="#99CCCC", url=""
})
.ToList();
Console.WriteLine(ObjCal);
note that you have to add using System.Linq
if you go for the second solution :)
CodePudding user response:
You are initializing your List in every interaction of your loop.
You just need to move the initialization of your List outside the loop and add the method .Add();
inside your loop.
Like below:
var monthlySchedule = db.StudioSchedules.ToList();
List<CalenderColumns> ObjCal = new List<CalenderColumns>();
ObjCal = new List<CalenderColumns>();
foreach (var item in monthlySchedule)
{
ObjCal.Add(
new CalenderColumns {
id = item.ID, name = item.Description,
startdate = item.Date.ToString(), enddate=item.Date.ToString(),
starttime =item.StartTime, endtime=item.EndTime,
color ="#99CCCC", url="" }
});
}
Console.WriteLine(ObjCal);