给定以下对象:
public class Person
{
public string Name {get; set;}
public string Age {get; set;}
public list<string> Interests {get; set;}
}
这里有一个一行的Linq方法可以把它展平(我可以使用扩展方法),以便于如果我们有如下嵌套的情况:
var People = new List<Person>(){
new Person(){
Name = "Bill",
Age = 40,
Interests = new List<string>(){"Football", "Reading"}
},
new Person = new List<Person>(){
Name = "John",
Age = 32,
Interests = new List<string>(){ "Gameshows", "Reading"}
},
new Person = new List<Person>(){
Name = "Bill",
Age = 40,
Interests = new List<string>(){ "Golf"}
}
}
我们可以得到一个结果(即,如果其他属性匹配,则将内容添加到
Interests
列表属性中):{
{
Name = "Bill",
Age = 40,
Interests = {"Football", "Reading", "Golf"}
},
{
Name = "John",
Age = 32,
Interests = { "Gameshows", "Reading"}
}
}