简体   繁体   English

C#-Genereic List复制一行添加另一行

[英]C# - Genereic List copy one row add Another row

Am having a list of students and on condition i need to copy one row and add that row with minor change 我有一份学生名单,但条件是我需要复制一行并以较小的更改将其添加

for example: 例如:

Class Student having follwing properties 具有以下特性的班级学生

public class Student
{      
    public int Id { get; set; }     
    public string Name { get; set; }
    public string Section { get; set; }
    public int Marks { get; set; }
}

So while looping through List if marks = 90 i need to copy that row and add another row by updating section 因此,当标记= 90时遍历列表时,我需要复制该行并通过更新部分添加另一行

 foreach (var item in studentData)
 {
    if(item.Section == 90)
    {
        //I need add some logic and update section and copy this item fully 
        //Add this modified item as new item to studentData
    }
 }

If student class had 3 items initially 如果学生班最初有3个项目

1 "Sam" "A" 48
1 "John" "B" 68
1 "Broad" "A" 90

my expected output would be 我的预期输出是

1 "Sam" "A" 48
1 "John" "B" 68
1 "Broad" "A" 90
1 "Broad" "C" 90


//Where i added one more row modifying the section

What will be the easiest way to do this without much looping ? 没有太多循环的最简单方法是什么? am stuck!! 卡住了!

I believe am clear with the question atleast with example !! 我相信至少有例子的问题很清楚!!

Thanks 谢谢

You can't add items to the collections you're iterating, so just create another list, add your copies to it and in the end add items from that list to your studentData . 您不能将项目添加到要迭代的集合中,因此只需创建另一个列表,向其中添加副本,最后将列表中的项目添加到studentData

Something like this: 像这样:

var copies = new List<Student>();

foreach (var item in studentData)
{
    if(item.Section == 90)
    {
        var copy = new Student();
        copy.ID = item.ID;
        copy.Name = item.Name;
        copy.Marks = item.Marks;
        copy.Section = // your updates to section
        copies.Add(copy);
    }
}

studentData.AddRange(copies);

If you really want to duplicate Student s (as I am not sure if this is good design), you can use following LINQ query: 如果您确实要复制Student (因为我不确定这是否是好的设计),则可以使用以下LINQ查询:

list = list.AddRange(list.Where(x => x.Section  == 90)
                         .Select(x => new Student() 
                                      { 
                                         // here set fields as you wish
                                      }));

where in constructor you can create new user accordingly. 在构造函数中,您可以据此创建新用户。

You should probably change your data structure. 您可能应该更改数据结构。 Think objects, not table rows. 考虑对象,而不是表行。

For example: 例如:

public class Student
{      
    public int Id { get; set; }     
    public string Name { get; set; }
    public List<Grade> Grades { get; set; }
}

public class Grade
{      
    // Something
}
        for (int i = 0; i< studentData.Count; i++)
        {
            var item = studentData[i];
            if (item.Marks == "90")
            {                    
                studentData.Insert(i, new Student { Id = item.Id, Name = item.Name + "(smart one)", Section = item.Section, Marks = item.Marks});                    
                i++;
            }
        }

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

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