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
Post a Comment