C# Lists: How to copy elements from one list to another, but only certain properties -
so have list of objects number of properties. among these properties name , id. let's call object extendedobject. i've declared new list of different objects have only properties name , id. let's call object basicobject.
what i'd convert or copy (for lack of better words) list of extendedobject objects list of basicobject objects. know c# lists have lot of interesting methods can useful, wondered if there easy way effect of:
basicobjectlist = extendedobjectlist.somelistmethod<basicobject>(some condition here);
but realize may end looking nothing that. realize loop through list of extendedobjects, create new basicobject each extendedobject's name , id, , push onto list of basicobjects. hoping little more elegant that.
does have ideas? much.
it depends on how you'd construct basicobject
extendedobject
, use convertall
method:
list<basicobject> basicobjectlist = extendedobjectlist.convertall(x => new basicobject { id = x.id, name = x.name });
or, if prefer, use linq select
method , convert list:
list<basicobject> basicobjectlist = extendedobjectlist.select(x => new basicobject { id = x.id, name = x.name }).tolist();
Comments
Post a Comment