Using linq to return an object with a <object> member list

I'm still a little new to Linq to SQL. I am looking for a way to get all the members of an object using a single linq query. The problem I am facing is that one of the members of the class is a list of user objects. This happens something like this:

Grade:

public class RoomConfiguration { public int ConfigID {get;set;} public string ConfigName { get; set; } public int RowCount { get; set; } public int ColCount { get; set; } public int FillDirection { get; set; } public string BigDoor { get; set; } public string SmallDoor { get; set; } public List<LineConfig> Lines { get; set; } } 

I am looking for a linq query that will populate all members of this class, including Lines. The data for the rows comes from another table, for which I also defined a class.

Thanks to everyone who can have a solution for this. Help is much appreciated!

+1
source share
1 answer

It is a bit complicated without knowing how your tables are related, but you can do it:

 var configs = db.RoomConfigurations .Select( r => new RoomConfiguration { ConfigID = r.ConfigID, ConfigName = r.ConfigName, ... Lines = db.LineConfigs .Where( l => l.RoomConfigID == r.ConfigID ) .ToList() }); 

or

 var configs = db.RoomConfigurations .Join( db.LineConfigs, r => r.ConfigID, l => l.RoomConfigID, (r,l) => new { RoomConfig = r, LineConfigs = l } ) .GroupBy( j => j.RoomConfig ) .Select( g => new RoomConfiguration { ConfigID = g.Key.ConfigID, ConfigName = g.Key.ConfigName, ... Lines = g.LineConfigs.ToList() }); 
+3
source

All Articles