使用linq返回带有list< object>的对象.成员 [英] Using linq to return an object with a list<object> member
问题描述
我对Linq to SQL还是有些陌生.我正在寻找一种通过使用单个linq查询来获取对象的所有成员的方法.我遇到的问题是,类成员之一是自定义对象的列表.它是这样的:
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:
班级:
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查询,它将填充该类的所有成员,包括Lines. Lines的数据来自另一个表,我也为其定义了一个类.
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()
});
或
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()
});
这篇关于使用linq返回带有list< object>的对象.成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!