c# - Update List Concatanate string -
c# - Update List Concatanate string -
i have class
public class roomavail {     public int occupancy { get; set; }     public int childcount { get; set; }     public string childages { get; set; } }    i have list of roomavail i.e list rooms;
rooms = { new roomavail{occupancy =1,childcount =1,childages ="1" }           new roomavail{occupancy =2,childcount =2,childages ="1,2" }           new roomavail{occupancy =3,childcount =3,childages ="1,2,3" }         }    i have populated value of rooms.
i have list listage = {12,13,14,14}
my requirement : if occupancy in list =2 , should append value of listage in childages .
final output:
rooms = { new roomavail{occupancy =1,childcount =1,childages ="1" }           new roomavail{occupancy =2,childcount =2,childages ="1,2,12,13,14,15" }           new roomavail{occupancy =3,childcount =3,childages ="1,2,3" }         }    i want update rooms variable only.
i doing:
rooms.where(y => y.occupancy == 2)      .select(x => x.childages)      .aggregate((i, j) => + listage  + j);    thanks
you seek one:
// rooms occupancy value equal 2. rooms = rooms.where(x=>x.occupancy==2);  // iterate through selected rooms. foreach(var room in rooms) {     // build list of integers based on room's list age.     list<int> currentlistage = room.childages.split(',').select(int.parse).tolist();      // concat currentlistage listage, order resulting list ,     // build comma separated value list.     room.childages = string.join(",", currentlistage.concat(listage)                                                     .orderby(x=>x)                                                     .select(x=>x.tostring())                                                     .tolist()); }        c# linq lambda 
 
Comments
Post a Comment