gpt4 book ai didi

c# - 嵌套列表上的 Linq C#

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:00 25 4
gpt4 key购买 nike

我的层次结构如下所示:

public class GetCategoriesReply : BaseReply
{
public IEnumerable<Subsystem> Categories { get; set; }
}
public class Subsystem
{
public IEnumerable<cat1> cat1 { get; set; }
public string id { get; set; }
public string name { get; set; }
}
public class cat1
{
public IEnumerable<cat2> cat2 { get; set; }
public string id { get; set; }
public string name { get; set; }

}
public class cat2
{
public IEnumerable<cat3> cat3 { get; set; }
public string id { get; set; }
public string name { get; set; }

}
public class cat3
{
public string id { get; set; }
public string name { get; set; }

}

如果可能的话,我想从查询中获取 Cat3 列表。我试过这个但没有让它工作:

var cat3list = categoryList.Categories
.Where(s => s.name == "Payment")
.Select(x => x.cat1.Where(c => c.name == "Admin").SelectMany(y => y.cat2.Select(z => z.cat3))
.ToList());

有什么建议吗?亲切的问候/鲁迪

最佳答案

var cat3list = Categories.Where(s => s.name == "Payment")
.SelectMany(x => x.cat1).Where(c => c.name == "Admin")
.SelectMany(x => x.cat2)
.SelectMany(x => x.cat3).ToList();

关于c# - 嵌套列表上的 Linq C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52666520/

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