Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add data from 1 list to another C#

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

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Then I make a new list called ObjCal from a public class

enter image description here

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
enter image description here

Goal: To have all 4 rows of data in ObjCal

>Solution :

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>()). 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:

However what you want to do is to add each item to the existing list. This can be done using the Add() method of your ObjCal list like so:

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 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading