vb.net - Using LINQ to combine object and nested collection information into a new object -


using linq, possible combine properties both object , nested collection of object new object? each item in nested collection, want create new object has nested object information coupled parent object's info.

using sample scenario, i'm trying this:

teachers.select(function(item) new teacherrecord()                                  {.teacherid = item.id,                                    .teachername = item.name,                                    .studentid = ? ,                                    .studentname = ?}).tolist() 

sample classes

public property teachers list(of teacher)   public class teacher     public property id integer     public property name string     public property room string     public property students list(of student) end class  public class student     public property id integer     public property name string end class  public class teacherrecord     public property teacherid integer     public property teachername string     public property studentid integer     public property studentname string end class 

you need use selectmany, don't know vb how in c#:

list<teacherrecord> records = teachers.selectmany(t => t.students, (t, s) =>                               new teacherrecord { teacherid = t.id,                                    teachername = t.name,                                    studentid = s.id,                                    studentname = s.name }).tolist(); 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -