c# - linq lambda convert object with list of objects of the same type to another object -


i have list<originalitem> need convert list<newitem>.

here classes

public class originalitem {    public int itemindex {get;set;}     public string name {get;set;}     private originalitem[] itemslist;     public originalitem[] itemslist    {                 {             return itemslist;         }         set         {             itemslist= value;         }    }  }  public class newitem {   public int newitemindex {get;set;}   public string newname {get;set;}    private newitem[] itemslist;    public newitem[] itemslist   {             {           return itemslist;       }       set       {          itemslist= value;       }    } } 

i know using select statement can create new object list i.e.

list<newitem> newitems = originalitems.select(x=> new newitem(){                                                             newitemindex = x.itemindex,                                                             newitemname = x.name                                                            }).tolist(); 

but how create list within list? doesnt have use recursion, if there way it.

thanks,

from above managed need minor changes:

public class originalitem { . . .    //add new method convert originalitem newitem    public newitem createnewitem()    {       newitem item = new newitem();       item.newname = this.name;       item.newitemindex = this.itemindex;       item.itemslist = this.itemslist.select(x =>x.createnewitem()).tolist();    } } 

and in main class had list<originalitem> originalitems did following:

list<newitem> newitems = originalitems.select(x=>x.createnewitem()).tolist(); 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -