c# - Manually adding member to a generic list after populating it with JSON data -


i using class goes list.

public class project {     public string id { get; set; }     public string title { get; set; }     public string status { get; set; }     public string externalurl {get; set; } //new } 

i populate list json data this:

projects = data["value"].toobject<list<project>>(); 

the json data matches members in class except new 1 added called externalurl.

so id, title, status json, i'd build externalurl using id json. this:

externalurl = "http://yoursite.com/v/a/" + id; 

so list contain bunch of objects this:

id title status externalurl

i tried this:

    foreach (project project in projects)     {         project.externalurl = "http://yoursite.com/" + project.id;       }      return projects; 

this does work, seemed slow down significantly.

is there more efficient/better way of doing this?

thanks!

can you?

public class project{      public string id { get; set; }      public string title { get; set; }      public string status { get; set; }      public string getexternalurl() {          return "http://yoursite.com/" + id;       } } 

edit: as side note, if access same project instance more once, better construct external url string once instead of recreating every time, new code should

public class project{      public string id { get; set; }      public string title { get; set; }      public string status { get; set; }       string _externalurl;      public string getexternalurl() {          if(string.isnullorwhitespace(_externalurl))              _externalurl = "http://yoursite.com/" + id;            return _externalurl;      } } 

more ugly, more efficient if access same instance multiple times


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -