質問

Name および AreaID プロパティを持つ Person クラスがあります。

public class Person
{
   public string Name;
   public int AreaID;

   // snip
}

List< Person> があり、リストに何百ものPersonオブジェクトが含まれる可能性があります。 例:AreaID = 1の100人とAreaID = 2の100人

AreaIDの個別のリストと、そのAreaIDを持っている人の数を返します。

たとえば、 AreaID = 1 Persons = 100 AreaID = 2 Persons = 100

役に立ちましたか?

解決

エリアIDでグループ化するように見えます:

var groups = from person in persons
             group 1 by person.AreaID into area
             select new { AreaID = area.Key, Persons = area.Count() };

「グループ1」を使用しています私は本当に各グループ内のデータを気にしないことを示すために-カウントとキーのみ。

これは、グループ化のためにすべての結果をバッファリングする必要があるという点で非効率的です-。 http://msmvps.com/blogs/jon_skeet/archive/2008/01/04/quot-push-quot-linq-revisited-next-attempt-at-an-explanation.aspx "rel =" nofollow noreferrer ">プッシュ必要に応じてLINQ 。繰り返しますが、比較的小さなデータセットの場合、おそらく重要ではありません:)

他のヒント

GroupByメソッドを使用します。

var list = ...list of Persons...

var areas = list.GroupBy( p => p.AreaID )
                .Select( g => new {
                    AreaID = g.Key,
                    Count = g.Count()
                 });

驚くべきことに、 Equals および GetHashCode をオーバーライドすることを勧める人はいませんでした。その場合、次のことができます。

 List<Person> unique = personList.Distinct();

または

 List<Person> areaGroup = personList.GroupBy(p => p.AreaID);
 List<Person> area1Count = personList.Where(p => p.AreaID == 1).Count();

これにより、柔軟性が向上します。無用な匿名クラスは不要です。

return list.GroupBy(p => p.AreaID)
    .Select(g => new { AreaID = g.Key, People = g.Count() });

list.GroupBy(x =&gt; x.AreaID); を使用できます

これを試すことができます:

var groups = from person in list
             group person by person.AreaID into areaGroup
             select new {
                 AreaID = areaGroup.Key,
                 Count = areaGroup.Count()
             };
        var people = new List<Person>();

        var q = from p in people
                group p by p.AreaId into g
                select new { Id = g.Key, Total = g.Count() };


        people.Add(new Person { AreaId = 1, Name = "Alex" });
        people.Add(new Person { AreaId = 1, Name = "Alex" });
        people.Add(new Person { AreaId = 2, Name = "Alex" });
        people.Add(new Person { AreaId = 3, Name = "Alex" });
        people.Add(new Person { AreaId = 3, Name = "Alex" });
        people.Add(new Person { AreaId = 4, Name = "Alex" });
        people.Add(new Person { AreaId = 2, Name = "Alex" });
        people.Add(new Person { AreaId = 4, Name = "Alex" });
        people.Add(new Person { AreaId = 1, Name = "Alex" });

        foreach (var item in q)
        {
            Console.WriteLine("AreaId: {0}, Total: {1}",item.Id,item.Total);
        }

このようなもの、おそらく?

            List<Person> persons = new List<Person> ();
            persons.Add (new Person (1, "test1"));
            persons.Add (new Person (1, "test2"));
            persons.Add (new Person (2, "test3"));

            var results = 
                persons.GroupBy (p => p.AreaId);

            foreach( var r in results )
            {
                Console.WriteLine (String.Format ("Area Id: {0} - Number of members: {1}", r.Key, r.Count ()));
            }

            Console.ReadLine ();

ToLookup()は必要な処理を行います。

scroll top