Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 理解LINQ子查询_C#_List_Linq_Lambda - Fatal编程技术网

C# 理解LINQ子查询

C# 理解LINQ子查询,c#,list,linq,lambda,C#,List,Linq,Lambda,我在foreach中使用了一个普通的查询来实现我的目标,但我认为有更好的方法 int max = 0; foreach(Area area in myZoo.AreaList) { max = (from a in area.AnimalList select a.ID).Max(); } return max; 如何使用纯LINQ获取所有子列表的max?(Animal是类动物的列表,位于区域列表内,包含一个名为ID的Int32)Erm,欢迎使用Lambda,因此

我在
foreach
中使用了一个普通的查询来实现我的目标,但我认为有更好的方法

int max = 0;
foreach(Area area in myZoo.AreaList)
{
    max = (from a in area.AnimalList
           select a.ID).Max();
}
return max;
如何使用纯LINQ获取所有子列表的
max
?(
Animal
类动物的
列表
,位于
区域列表
内,包含一个名为
ID
Int32
)Erm,欢迎使用Lambda,因此不要因为只知道Lambda答案而匆忙回答;)

公共类动物园
{
公共列表区域列表{get;set;}
}
公共课区
{
公共列表动物列表{get;set;}
}
公营动物
{
公共列表Id{get;set;}
}

只有简短的形式,所以没有人会感到困惑;)

您正在寻找嵌套的

SelectMany
将从多个“内部”IEnumerable
-so
Zoo返回单个
IEnumerable
。SelectMany(a=>a.AreaList)
将返回一个
IEnumerable
,其中包含
Area
属性中的所有
IEnumerable
,然后您将在
Area
类中的
列表中再次执行此操作:

样本数据:

var zoo = new Zoo() {
    AreaList = new List<Area>()
    {
        new Area() 
        {
            AnimalList = new List<Animal>()
            {
                new Animal() {Id = new List<int>() {1, 2, 3}},
                new Animal() {Id = new List<int>() {4, 5, 6}}
            }
        },
        new Area() 
        {
            AnimalList = new List<Animal>()
            {
                new Animal() {Id = new List<int>() {7, 8, 9}},
                new Animal() {Id = new List<int>() {10, 11}}
            }
        },
    }
};
结果:11


在查询语法中,您可以通过链接来自
子句的
来执行
SelectMany
,如gxp的答案所示。(就我个人而言,我更喜欢方法链接语法,所以我花了一些时间才找到答案…

虽然
SelectMany
有效,但我觉得在这种情况下查询更具可读性

var max = (from area in myZoo.AreaList
           from animal in area.AnimalList
           from id in animal.Id
           select id).Max();
所有
动物列表的所有最大值的最大值以及它们的最大值


这与ZoharPeled的
SelectMany
非常有效,只是他将列表展平并取所有项目的最大值,而我一次又一次地取每个列表的最大值。

很好!我希望我自己也能想到这一点:-)谢谢。我不知道的那个(从x中的y到x.a中的a)对我来说是新的,谢谢。很高兴能帮上忙:-)@ZoharPeled我想出了这个
zoo.AreaList.SelectMany(x=>x.AnimalList.Select(y=>y.Id.Max()).Max()解决方案,但由于已选择答案,我不会发布它。但你能解释一下你的方法和我的方法之间的比较吗?这基本上是我和z的结合ᴉɹɥƆ的答案-你做一个
SelectMany
来压平一个列表,然后得到最大值的最大值。@ZoharPeled是的,通过阅读所有答案(这就是为什么我没有回答),我知道了这一点,我也阅读了你对z的评论ᴉɹɥƆ的回答。我在问这个“
SelectMany
将一个列表展平,然后得到最大值中的最大值”,结果是否有助于提高空间复杂度?@Amit我不知道,我认为这并不重要。您应该编写代码以确保可读性。空间复杂性在2018年应该无关紧要,除非是在海量数据中工作。
var max = zoo.AreaList.SelectMany(a => a.AnimalList).SelectMany(a => a.Id).Max();

Console.WriteLine(max);
var max = (from area in myZoo.AreaList
           from animal in area.AnimalList
           from id in animal.Id
           select id).Max();
var max = zoo.AreaList.Max(arl => arl.AnimalList.Max(anl => anl.Id)).Max();