简体   繁体   中英

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

在此处输入图片说明

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

在此处输入图片说明

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

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM