关于c#:在LINQ中分组

Group by in LINQ

假设我们有一个像

1
2
3
4
class Person {
    internal int PersonID;
    internal string car  ;
}

现在我有一张这个班的单子:List persons;

现在,此列表可以有多个具有相同个人ID的实例,例如:

1
2
3
persons[0] = new Person { PersonID = 1, car ="Ferrari" };
persons[1] = new Person { PersonID = 1, car ="BMW"     };
persons[2] = new Person { PersonID = 2, car ="Audi"    };

有没有办法让我按personID分组,得到他所有汽车的清单?

例如,预期结果是

1
2
3
4
class Result {
   int PersonID;
   List<string> cars;
}

所以分组后,我会得到:

1
2
3
4
5
results[0].PersonID = 1;
List<string> cars = results[0].cars;

result[1].PersonID = 2;
List<string> cars = result[1].cars;

从我迄今为止所做的工作来看:

1
2
3
var results = from p in persons
              group p by p.PersonID into g
              select new { PersonID = g.Key, // this is where I am not sure what to do

有人能给我指个方向吗?


当然-你基本上想要:

1
2
3
var results = from p in persons
              group p.car by p.PersonId into g
              select new { PersonId = g.Key, Cars = g.ToList() };

或作为非查询表达式:

1
2
3
4
var results = persons.GroupBy(
    p => p.PersonId,
    p => p.car,
    (key, g) => new { PersonId = key, Cars = g.ToList() });

基本上,组的内容(当视图为IEnumerable时)是给定键的投影(本例中为p.car中的任何值)的序列。

有关GroupBy如何工作的更多信息,请参阅我在该主题上的edulinq文章。

(为了遵循.NET命名约定,我在上面将PersonID重命名为PersonID

或者,您可以使用Lookup

1
var carsByPersonId = persons.ToLookup(p => p.PersonId, p => p.car);

然后你可以很容易地为每个人得到汽车:

1
2
// This will be an empty sequence for any personId not in the lookup
var carsForPerson = carsByPersonId[personId];


1
2
3
4
var results = from p in persons
              group p by p.PersonID into g
              select new { PersonID = g.Key,
                           /**/car = g.Select(g=>g.car).FirstOrDefault()/**/}


1
2
3
var results = from p in persons
              group p by p.PersonID into g
              select new { PersonID = g.Key, Cars = g.Select(m => m.car) };

你也可以试试这个。

1
2
3
4
var results= persons.GroupBy(n => new { n.PersonId, n.car})
                .Select(g => new {
                               g.Key.PersonId,
                               g.Key.car)}).ToList();

尝试

1
persons.GroupBy(x => x.PersonId).Select(x => x)

要检查列表中是否有人重复,请尝试

1
persons.GroupBy(x => x.PersonId).Where(x => x.Count() > 1).Any(x => x)

我用查询语法和方法语法创建了一个工作代码示例。我希望它能帮助其他人:)

您也可以在.NET上运行代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Linq;
using System.Collections.Generic;

class Person
{
    public int PersonId;
    public string car  ;
}

class Result
{
   public int PersonId;
   public List<string> Cars;
}

public class Program
{
    public static void Main()
    {
        List<Person> persons = new List<Person>()
        {
            new Person { PersonId = 1, car ="Ferrari" },
            new Person { PersonId = 1, car ="BMW" },
            new Person { PersonId = 2, car ="Audi"}
        };

        //With Query Syntax

        List<Result> results1 = (
            from p in persons
            group p by p.PersonId into g
            select new Result()
                {
                    PersonId = g.Key,
                    Cars = g.Select(c => c.car).ToList()
                }
            ).ToList();

        foreach (Result item in results1)
        {
            Console.WriteLine(item.PersonId);
            foreach(string car in item.Cars)
            {
                Console.WriteLine(car);
            }
        }

        Console.WriteLine("-----------");

        //Method Syntax

        List<Result> results2 = persons
            .GroupBy(p => p.PersonId,
                     (k, c) => new Result()
                             {
                                 PersonId = k,
                                 Cars = c.Select(cs => cs.car).ToList()
                             }
                    ).ToList();

        foreach (Result item in results2)
        {
            Console.WriteLine(item.PersonId);
            foreach(string car in item.Cars)
            {
                Console.WriteLine(car);
            }
        }
    }
}

结果如下:

1
2
3
4
5
6
7
8
9
10
11
1
Ferrari
BMW
2
Audi
-----------
1
Ferrari
BMW
2
Audi


试试这个:

1
2
3
4
var results= persons.GroupBy(n => n.PersonId)
            .Select(g => new {
                           PersonId=g.Key,
                           Cars=g.Select(p=>p.car).ToList())}).ToList();

但是从性能上讲,下面的实践在内存使用方面更好、更优化(当我们的数组包含更多像数百万这样的项目时):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var carDic=new Dictionary<int,List<string>>();
for(int i=0;i<persons.length;i++)
{
   var person=persons[i];
   if(carDic.ContainsKey(person.PersonId))
   {
        carDic[person.PersonId].Add(person.car);
   }
   else
   {
        carDic[person.PersonId]=new List<string>(){person.car};
   }
}
//returns the list of cars for PersonId 1
var carList=carDic[1];


1
var results = persons.GroupBy(n => n.PersonId).Select(r => new Result {PersonID = r.Key, Cars = r.ToList() }).ToList()