我有以下课程:
public class Owner
{
public string Id { get; set; }
public string Name { get; set; }
}
public class Main
{
public string Id { get; set; }
public string Name { get; set; }
public List<Owner> Owners { get; set; }
}
我想将List<Main>
转换为FlatList为
List<FlatList>
public class FlatList
{
public string Id { get; set; } // Id from Main
public string Name { get; set; } // Name from Main
public string OwnerId { get; set; } // Id from each Owner in a Main's Owner
public string OwnerName { get; set; } // Name from each Owner in a Main's Owner
}
不幸的是,我无法弄清楚LinQ查询来执行此操作。
答案 0 :(得分:3)
您应该使用SelectMany展平一系列主要对象:
将序列的每个元素投影到IEnumerable并展平 得到的序列成一个序列。
因此,它将每个Main对象投影为FlatList
个对象的序列,然后将生成的序列展平为一个FlatList
序列
var flatList = mainList.SelectMany(m =>
m.Owners.Select(o =>
new FlatList {
Id = m.Id,
Name = m.Name,
OwnerId = o.Id,
OwnerName = o.Name
})).ToList()
答案 1 :(得分:3)
MediaStore.Images.ImageColumns.DATE_TAKEN
答案 2 :(得分:1)
你可以使用linq(幕后秘密循环)来做到这一点:
from m in mainList
from o in m.Owners
select new FlatList
{
Id = m.Id,
Name = m.Name,
OwnerId = o.OwnerId ,
OwnerName = o.OwnerName
};