c# - create an array of anonymous type -


i trying data database google charts in program. create array of anonymous type (var) instead of repeating code on , on again:

 public jsonresult getchartdata(int sid, int regionid)     {              var testpathone = p in _rep.getmetricsdata().getlhdb().page_loads                                                                p.t_3id == sid && p.test_path_id == 1                               select new { time = p.time, created_at = p.created_at };              var testpathtwo = p in _rep.getmetricsdata().getlhdb().page_loads                               p.t_3id == sid && p.test_path_id == 2                               select new { time = p.time, created_at = p.created_at };              var tone = testpathone.toarray();             var ttwo = testpathtwo.toarray();             var name = new { test1 = tone, test2 = ttwo };             return json(name);         } 

i know need loop can go through test path id's instead of hard coding them p.test_path_id == 1, question how make part dynamic var name = new { test1 = tone, test2 = ttwo };

edit: apologize, this:

name array  loop:     testpath = query     name.add(testpath) 

i hope makes sense

the easiest solution in particular case give name class anonymous. while there workarounds can use, when need start working hard use anonymous type shouldn't using it. it's there make tasks quicker , easier; if isn't happening better off real class.

that solution this:

//please give me real name public class classtoberenamed {     public datetime time { get; set; }     public datetime createdat { get; set; } }  list<classtoberenamed[]> mylist = new list<classtoberenamed[]>();  (int = 0; < 10; i++) { mylist.add((from p in _rep.getmetricsdata().getlhdb().page_loads             p.t_3id == sid && p.test_path_id ==             select new classtoberenamed { time = p.time, createdat = p.created_at })             .toarray()); } 

having said of that, it's still possible.

var mylist = new[]{                 p in _rep.getmetricsdata().getlhdb().page_loads                 p.t_3id == sid && p.test_path_id == 1                 select new { time = p.time, created_at = p.created_at }.toarray() }.tolist();  (int = 2; < 10; i++) {     mylist.add(from p in _rep.getmetricsdata().getlhdb().page_loads                 p.t_3id == sid && p.test_path_id ==                 select new { time = p.time, created_at = p.created_at }.toarray()                 ); }  var myarray = mylist.toarray(); 

if it's really, important have array, , not list, call toarray on mylist @ end. it's important start out list, , convert array @ end because arrays have fixed size once created. can mutate contents, can't make them bigger or smaller. list on other hand, designed mutate it's size on time, can start out 0 or 1 items , add items on time, important in particular context. (it's useful quite often, why useful use list on arrays.)


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -