c# - LINQ collapse multiple rows into List -


i have following objects manage sending emails groups:

public class groupemailrecipients {      public string groupname { get; set; }       public list<emailrecipient> emailrecipients { get; set; }       public groupemailrecipients()      {         emailrecipients = new list<emailrecipient>();      } }  public class emailrecipient {     public string email { get; set; }      public string firstname { get; set; }      public string lastname { get; set; } } 

i have table called groupcontacts contains:

groupname | email | firstname | lastname name01 | john.smith@test.com | john | smith name01 | jane.doe@test.com | jane | doe name02 | bill.smith@test.com | bill | smith 

is possible create linq statement populates list<groupemailrecipients> groupcontacts table?

the list<groupemailrecipients> populated as:

 first groupemailrecipients object:   groupname = "name01"  emailrecipients = { email= "john.smith@test.com", firstname="john",   lastname="smith"  } , { email= "jane.doe@test.com",  firstname="jane",  lastname = "doe"  }   second groupemailrecipients object:   groupname = "name02"  emailrecipients = { email= "bill.smith@test.com", firstname="bill",  lastname="smith"  } 

i'm using entityframework 6.0, stub like:

 list<groupemailrecipients> results = new list<groupemailrecipients>();   using (var db = new maincontext())  {      results = db.groupcontacts.[linq syntax].tolist();  } 

this should work.

db.groupcontacts.groupby(r => r.name)                 .select(group => new groupemailrecipients()                      { groupname = group.key,                        emailrecipients = group.select(x => new emailrecipient()                           {                              firstname = x.firstname,                              lastname = x.lastname,                              email = x.email                            })                     }); 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -