简体   繁体   English

了解LINQ子查询

[英]Understanding LINQ subquery

I used a normal query inside a foreach to reach my goal, but I think there is a better way. 我在foreach使用了普通查询来达到目标​​,但是我认为有更好的方法。

int max = 0;
foreach(Area area in myZoo.AreaList)
{
    max = (from a in area.AnimalList
           select a.ID).Max();
}
return max;

How can I use a pure LINQ to get the max of all sublists? 如何使用纯LINQ获取所有子列表的max ( Animal is a List of the class Animal inside the AreaList and contains an Int32 called ID ) Erm, and Lambda is welcome, so don't hastitate to answer only because you only know the lambda answer ;) AnimalAreaList内的class AnimalList ,并且包含一个称为IDInt32AreaList和Lambda是受欢迎的,因此不必着急回答,因为您只知道lambda回答;)

public class Zoo
{
    public List<Area> AreaList {get; set;}
}

public class Area
{
    public List<Animal> AnimalList {get; set;}
}

public class Animal
{
    public List<int> Id {get; set;}
}

Only the short form so no one gets confused ;) 只有简短的形式,所以没有人会混淆;)

Although SelectMany works, I feel query is more readable in such case. 尽管SelectMany可以工作,但我觉得在这种情况下查询更易读。

var max = (from area in myZoo.AreaList
           from animal in area.AnimalList
           from id in animal.Id
           select id).Max();

You are looking for a nested SelectMany . 您正在寻找嵌套的SelectMany

SelectMany will return a single IEnumerable<T> from the many "inner" Ienumerable<T> - so Zoo.SelectMany(a => a.AreaList) will return a single IEnumerable<Area> containing all the IEnumerable<Area> from the Area property - then you do it one more time for the List<Animal> in the Area class: SelectMany将返回一个单一IEnumerable<T>从许多“内” Ienumerable<T> -所以Zoo.SelectMany(a => a.AreaList)将返回一个单一IEnumerable<Area>包含所有IEnumerable<Area>Area属性-然后对Area类中的List<Animal>再做一次:

Sample data: 样本数据:

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}}
            }
        },
    }
};

Linq query: Linq查询:

var max = zoo.AreaList.SelectMany(a => a.AnimalList).SelectMany(a => a.Id).Max();

Console.WriteLine(max);

Result: 11 结果:11

In query syntax you can do SelectMany by chaining from clauses, as shown in gxp's answer. 在查询语法中,您可以通过链接from子句来执行SelectMany ,如gxp的答案所示。 (Personally, I prefer the method chaining syntax, so it took me some time to figure that one out...) (个人而言,我更喜欢方法链接语法,因此花了一些时间才弄清楚方法...)

var max = zoo.AreaList.Max(arl => arl.AnimalList.Max(anl => anl.Id)).Max();

The maximum value of all maximum values of all AnimalLists , and the Max of them. 所有AnimalLists的所有最大值的最大值及其最大值。

That's effectievly the same as ZoharPeled's SelectMany , except he flattens the list and takes the max of all items where I take the max of each list again and again. 这实际上与ZoharPeled的SelectMany相同,除了他拉平列表并取所有项目的最大值,而我一次又一次取每个列表的最大值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM