gpt4 book ai didi

c# - 如何通过 lambda/方法表达式使用 linq 创建嵌套组

转载 作者:行者123 更新时间:2023-12-02 04:50:13 27 4
gpt4 key购买 nike

这可能吗?例如,您将如何使用 lambda 表达式重写下面的工作代码?

public void QueryNestedGroups()
{
var queryNestedGroups =
from student in students
group student by student.Year into newGroup1
from newGroup2 in
(from student in newGroup1
group student by student.LastName)
group newGroup2 by newGroup1.Key;
}

// Three nested foreach loops are required to iterate
// over all elements of a grouped group. Hover the mouse
// cursor over the iteration variables to see their actual type.
foreach (var outerGroup in queryNestedGroups)
{
Console.WriteLine("DataClass.Student Level = {0}", outerGroup.Key);
foreach (var innerGroup in outerGroup)
{
Console.WriteLine("\tNames that begin with: {0}", innerGroup.Key);
foreach (var innerGroupElement in innerGroup)
{
Console.WriteLine("\t\t{0} {1}", innerGroupElement.LastName, innerGroupElement.FirstName);
}
}
}

最佳答案

var names = myClass1List
.SelectMany(c1 => c1.Class2List.Where(c2 => c2.Name == "something"))
.SelectMany(c2 => c2.Class3List.Select(c3 => c3.Name));

var names = myClass1List
.SelectMany(c1 => c1.Class2List
.Where(c2 => c2.Name == "something")
.SelectMany(c2 => c2.Class3List
.Select(c3 => c3.Name)));

查看更多:LINQ on List with lot of nested List

关于c# - 如何通过 lambda/方法表达式使用 linq 创建嵌套组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989169/

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