c# - Populate object from list of objects -


i have following rootobject object need populate.

public class document2 {     public string homecommunityid { get; set; }     public string repositoryid { get; set; }     public string documentuuid { get; set; }     public string doctype { get; set; } } public class document {     public document2 document { get; set; } }     public class rootobject {     public list<document> documents { get; set; } } 

i need populate rootobject object list of following documentquery object.

public class documentquery {         public string doctype { get; set; }         public string homecommunityid { get; set; }         public string repositoryid { get; set; }         public string documentuuid { get; set; }  } 

i trying follows:

rootobject doc = new rootobject()    {       document= obj.select(s => new document()       {           document = new document2()              {                  doctype = s.doctype               }        })     }; 

i need able json in format:

    {   "documents": [     {       "document": {         "homecommunityid": "",         "repositoryid": "",         "documentuuid": "",         "doctype": ""       }     }   ] } 

you can use following code, if don't want play reflection etc.:

list<documentquery> queries = new list<documentquery>(); // fill queries rootobject root = new rootobject(); foreach(var query in queries) {     document document = new document()     {         document = new document2()         {             homecommunityid = query.homecommunityid,             repositoryid = query.repositoryid,             documentuuid = query.documentuuid,             doctype = query.doctype         }     }     root.documents.add(document); } 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -