gpt4 book ai didi

c# - 如何从人员列表中查询孙辈?

转载 作者:行者123 更新时间:2023-12-03 03:16:07 25 4
gpt4 key购买 nike

在下面的代码中,我已经解释了我想要的内容,请有人帮助我解决这个问题。

public class Person
{
public IEnumerable<Child> Children { get; set; }
}

public class Child
{
public IEnumerable<GrandChild> GrandChildren { get; set; }
}

public class SearchingClass
{
public void Search()
{
IEnumerable<Person> persons = MyPersons;
IEnumerable<GrandChild> grandChildren = MyGrandChildren

//Now I want only the Grand Children who are grand children of persons in Persons List
//How can I write a query for this ?
}
}

最佳答案

perons.SelectMany(p => p.Children)
.SelectMany(c => c.GrandChildren);
// add Distinct() if you need distinct results

如果您只需要获取 grandChildren 集合中的孙子:

grandChildren.Intersect(
perons.SelectMany(p => p.Children)
.SelectMany(c => c.GrandChildren));

关于c# - 如何从人员列表中查询孙辈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20189088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com