1

[同じタイプ] の子供を持つ人物のリストがあります。xml ファイルからリストを取得します。

シナリオ :

Person : ID、名前、性別、年齢、子供 [フィールドを持つクラス]

personList の ID が 1、2、5 の場合、
2 と 5 にそれぞれ子 3、4 と 6、7、8 があります。
最大IDを8として取得する必要があります。

ラムダ式を使用して PersonList から Id の最大値を取得するにはどうすればよいですか?

4

2 に答える 2

5

と の組み合わせを試すことができますConcat(これは、 SelectMany1 つのレベルだけがネストされていることを前提としています)。

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

更新
複数の入れ子レベルがある場合は、拡張メソッドを記述してSelectMany再帰的にすることもできます。

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

それは循環参照を処理せずSelectMany、ソース コレクション自体のアイテムを返す場合とは動作が異なります (そのため、名前を変更したい場合があります)。非常に簡単に使用できます。

var maxId = personList.SelectManyRecursive(p => p.Children).Max(p => p.Id);
于 2012-06-12T07:27:59.500 に答える
0

別のレベルを追加して、シナリオをわずかに切り替えました。これで問題が解決しない場合は、データ オブジェクトの例を投稿してください。

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;
        }
    }
}
于 2012-06-12T17:28:30.310 に答える