Serialize list objects to JSON using C# -


i need suggestions on how import data multiple list objects using dapper , serialize lists proper json format. please share suggestions. thanks!

1. sample json format

{   "students": [     {       "studentid": "",       "emailaddresses": [         {           "emailaddress": ""         }       ],             "lastname": "",       "directory": {         "addresses": [           {             "city": "",             "state": ""           }         ],             }          }   ] } 

2. classes

public class student {     public string studentid { get; set; }     public string gender { get; set; }       public list<emailaddresses> emailaddresses { get; set; } }  public class emailaddresses {     public string emailaddress { get; set; }     } 

3. data coming in sql query

studentid   gender  emailaddress 123456  female  maryjoe@gmail.com 123456  female  mary.joe@mycollege.edu 123456  female  mj@hotmail.com 

4. dapper code trying implement

 public list<student> getstudentdata()  {      list<student> datastudent;      using (idbconnection connection = repositoryhelper.openconnection())      {          datastudent = connection.query<student, emailaddresses, student>("mystoredprocedure",             (c, cc) =>             {                 c.emailaddresses = cc;                 return c;             }, spliton: "emailaddress").tolist();          }      return datastudent; } 

4. serializing json

static void main(string[] args) {                 list<student> students = getstudentdata();     var json = new system.web.script.serialization.javascriptserializer()                                                   .serialize(students); } 

the first issue invoking stored procedure without specifying command type stored procedure. mis-translate proc name sql command fail.

however, based on updated comment, second problem mis-using dapper's multi mapper functionality. functionality mapping multiple tables multiple objects. however, seems stored procedure giving flattened object , need break apart unique records based on student id, mapping email addresses email address property.

dynamic works since can map data on fly.

public list<student> getstudentdata() {     list<student> datastudent;     using (idbconnection connection = repositoryhelper.openconnection())     {         datastudent = connection.query<dynamic>(             "mystoredprocedure",              commandtype: commandtype.storedprocedure)                 .groupby(x => x.studentid)                 .select(x => new student                      {                          studentid = x.first().studentid,                          gender = x.first().gender,                         emailaddresses = x.select(ea => new emailaddresses                              {                                  emailaddress = ea.emailaddresses                              }).tolist()                     }).tolist();          return datastudent;     } } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -