Find Max Id from list which has children of same list

I have a list of Persons which has children [same type]. I get the list from xml file.

Scenario :

Person : Id, Name, Gender, Age, Children [Class with fields]

If personList has 1,2,5 Ids,
2 and 5 with children 3,4 and 6,7,8 respectively.
I have to get the max id as 8.

How do i get the max of Id from PersonList using lambda expression?


You could try a combination of Concat and SelectMany (that's assuming it's only nested one level):

var maxId = personList.Concat(personList.SelectMany(p => p.Children)).Max(p => p.Id);

UPDATE
If you have multiple nesting levels, you could also write an extension method to make SelectMany recursive:

public static IEnumerable<T> SelectManyRecursive<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> selector) {
    if (source == null) { yield break; }
    foreach (var item in source) {
        yield return item;
        foreach (var selected in selector(item).SelectManyRecursive(selector)) {
            yield return selected;
        }
    }
}

That doesn't handle circular references and it behaves differently than SelectMany by also returning the items in the source collection itself (so you might want to change the name), but otherwise i think it does the job. You could use it quite easily:

var maxId = personList.SelectManyRecursive(p => p.Children).Max(p => p.Id);

Take a look here..

http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/84c720e0-a734-4320-9cf7-b9a198a25184/


I switched your scenario sligthly by adding another level. If this does not help please post an example of your data objects.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace App
{
    public enum ID{
        one, two, three, four, five, six, seven, eigth, nine, ten
    }

    public class Person
    {
        public ID id;
        public List<Person> children;
        public Person(ID id, List<Person> children)
        {
            this.id = id;
            this.children = children;
        }
    }

    class Program
    {
        private static List<Person> BuildScenario()
        {
            return new List<Person>{
                new Person(ID.one, new List<Person>()),
                new Person(ID.two, new List<Person>{
                    new Person(ID.three, new List<Person>{
                        new Person(ID.ten, new List<Person>())
                    }),
                    new Person(ID.four, new List<Person>())
                }),
                new Person(ID.five, new List<Person>{
                    new Person(ID.six, new List<Person>()),
                    new Person(ID.seven, new List<Person>()),
                    new Person(ID.eigth, new List<Person>())
                })
            };
        }

        static void Main(string[] args)
        {
            List<Person> scenario = BuildScenario();
            Console.WriteLine(CountIDs(scenario).ToString());
            Console.WriteLine(GetMaxID(scenario).ToString());
            while(true);
        }

        private static int CountIDs(List<Person> scenario)
        {
            int count = 0;
            foreach (Person person in scenario)
            {
                count += 1 + CountIDs(person.children);
            }
            return count;
        }


        private static ID GetMaxID(List<Person> scenario)
        {
            ID maxid = 0;
            foreach(Person person in scenario)
            {
                ID childmax = GetMaxID(person.children);
                if (person.id > maxid) maxid = person.id;
                if (childmax > maxid) maxid = childmax;

            }
            return maxid;
        }
    }
}
链接地址: http://www.djcxy.com/p/51374.html

上一篇: 将XML加载到下拉列表中C#

下一篇: 从具有相同列表的子项的列表中查找最大ID