public class ParentType
{
public int ID;
public string Name;
public List<ChildType> ChildTypes { get; set; }
}
public class ChildType
{
public int ID;
public string Name;
}
public class MixedType
{
public int ParentID;
public string ParentName;
public int ChildID;
public string ChildName;
}
答案 0 :(得分:6)
如果我了解您要执行的操作,则必须使用this overload SelectManay扩展名,该扩展名允许您在其中的每个元素上调用结果选择器函数。
查询应为:
var lst = new List<ParentType>();
var query = lst.SelectMany(p => p.ChildTypes,
(parent, child) => new { parent, child }
)
.Select(p => new MixedType
{
ChildID = p.child.ID,
ChildName = p.child.Name,
ParentID = p.parent.ID,
ParentName = p.parent.Name
});
祝你好运!
答案 1 :(得分:5)
from p in parents
from c in p.Children
select new MixedType(...)
应该有效。
答案 2 :(得分:1)
我相信您正在尝试从ParentType
的一个实例中投射一个MixedType集合var parent = new ParentType();
var mixedList = parent.ChildTypes
.Select(c => new MixedType
{
ParentID = parent.ID,
ParentName = parent.Name,
ChildID = c.ID,
ChildName = c.Name
});
答案 3 :(得分:0)
如果我理解正确,你可以尝试这样的事情:
var mixed =
from child in parent.ChildTypes
select new MixedType
{
ParentID = parent.ID,
ParentName = parent.Name,
ChildID = child.ID,
ChildName = child.Name
};