使用List <person> Distinct()返回2个值</person>

时间:2009-10-22 14:30:41

标签: c# .net linq c#-3.0 ienumerable

我有一个Person课程,其中NameAreaID属性。

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

   // snip
}

我有一个List<Person>,可能会列出数百个Person对象。 例如,AreaID = 1的100人和AreaID = 2的100人

我想返回AreaID的不同列表以及有多少人拥有该AreaID。

例如, AreaID = 1人= 100 AreaID = 2人= 100

10 个答案:

答案 0 :(得分:6)

使用GroupBy方法。

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

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

答案 1 :(得分:5)

您希望按地区ID进行分组:

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

我正在使用“组1”来表示我真的不关心每个组中的数据 - 只有计数和密钥。

这是低效的,因为它必须为了分组而缓冲所有结果 - 你能够在.NET 4.0中使用Reactive LINQ来更有效地执行此操作,或者你当然可以使用Push LINQ如果你想。然后,对于相对较小的数据集,它可能无关紧要:)

答案 2 :(得分:1)

令人惊讶的是,没有人建议覆盖EqualsGetHashCode。如果你这样做,你可以做下面的事情:

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

甚至

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

这为您提供了更大的灵活性 - 无需无用的匿名课程。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

使用GroupBy或更简洁的LINQ语句而不是不同:

var results = from p in PersonList
              group p by p.AreaID into g
              select new { AreaID=g.Key, Count=g.Count() };

foreach(var item in results)
    Console.WriteLine("There were {0} items in Area {1}", item.Count, item.AreaID);

答案 5 :(得分:0)

您可以使用list.GroupBy(x => x.AreaID);

答案 6 :(得分:0)

你可以试试这个:

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

答案 7 :(得分:0)

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

答案 8 :(得分:0)

也许是这样的事情?

            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 ();

答案 9 :(得分:0)

ToLookup()会做你想做的事。