asp.net - C# - Genereic List copy one row add Another row -
am having list of students , on condition need copy 1 row , add row 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 need copy row , add row updating section
foreach (var item in studentdata) { if(item.section == 90) { //i need add logic , update section , copy item //add modified item new item studentdata } }
if student class had 3 items initially
1 "sam" "a" 48 1 "john" "b" 68 1 "broad" "a" 90
my expected output be
1 "sam" "a" 48 1 "john" "b" 68 1 "broad" "a" 90 1 "broad" "c" 90 //where added 1 more row modifying section
what easiest way without looping ? stuck!!
i believe clear question atleast example !!
thanks
you can't add items collections you're iterating, create list, add copies , in end add items list studentdata
.
something 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 = // updates section copies.add(copy); } } studentdata.addrange(copies);
Comments
Post a Comment