问题描述
我仍然是Linq到SQL的新事物.我正在寻找一种使用单个LINQ查询来获取对象的所有成员的方法.我遇到的问题是,一类成员是自定义对象的列表.它是这样的:
类:
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; } }
我正在寻找一个LINQ查询,该查询将填充此类的所有成员,包括行.线的数据来自我也定义了一个类的另一个表.
感谢任何可能有解决方案的人.帮助您非常感谢!
推荐答案
有点困难,不知道您的桌子如何相关,但是您可以做到这一点:
var configs = db.RoomConfigurations .Select( r => new RoomConfiguration { ConfigID = r.ConfigID, ConfigName = r.ConfigName, ... Lines = db.LineConfigs .Where( l => l.RoomConfigID == r.ConfigID ) .ToList() });
或
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() });
问题描述
I am still somewhat new to Linq to SQL. I am looking for a way to get all members of an object by using a single linq query. The problem I haev run into is that one of the class members is a list of custom objects. It goes something like this:
Class:
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 fill in all of the members of this class, including Lines. The data for Lines comes from another table which I have defined a class for as well.
Thank you to anyone who may have a solution for this. Help is much appreciated!
推荐答案
It's a little hard, not knowing how your tables relate, but you could do this:
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() });