简体   繁体   中英

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

 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

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 .

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:

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++;
            }
        }

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