简体   繁体   English

将 1 个列表中的数据添加到另一个 C#

[英]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.首先,我告诉程序将monthlySchedule放入列表中。 Which contains 4 rows of data其中包含4行数据

在此处输入图片说明

Then I make a new list called ObjCal from a public class然后我从公共课程中创建了一个名为 ObjCal 的新列表

在此处输入图片说明

Next, I write a foreach loop of monthlySchedule which contains the 4 rows to add it to ObjCal接下来,我编写了一个包含 4 行的monthlyScheduleforeach循环将其添加到ObjCal

The issue is the foreach loop is only getting the last row but I'm trying to get all 4 rows.问题是foreach 循环只获取最后一行,但我试图获取所有 4 行。 Shown below如下图在此处输入图片说明

Goal: To have all 4 rows of data in ObjCal目标:在 ObjCal 中拥有所有 4 行数据

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).您当前不是将项目添加到您的ObjCal列表中,而是为每个项目创建一个新列表(您在循环调用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.这个新创建的列表(它只包含对象初始值设定项中指定的一项)然后存储在ObjCal变量中。 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).这会重复多少次,因为您在monthlySchedule有元素,最后给您一个列表,该列表只包含monthlySchedule最后一个元素(由foreach循环的最后一次迭代产生)。

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).您想要做的是将每个项目添加到现有列表(您在foreach循环之前正确创建的)中。 Items can be added to a list by calling the Add() method and passing the item you want to add to it.可以通过调用Add()方法并传递要添加的项目来将项目添加到列表中。 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).当您想将所有元素添加到ObjCal ,对Add()的调用应放置foreach循环内(以便对所有元素重复此操作)。 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:或者如果您更喜欢 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 :)请注意,如果您采用第二种解决方案,则必须using System.Linq添加 :)

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();您只需要将 List 的初始化移动到循环之外并添加方法.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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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