LINQ添加或总结项目

0 c# linq asp.net-mvc-3

我一直试图找到我的问题的答案,但措辞很难.

假设我有以下内容:

  • 第一个政党表: party 1, party 2, party 3, party 4

  • 第二个表,记录谁属于哪一方:

    party 1 - alex, party 1 - cynthia
    party 2 - charles
    party 3 - ana, party 3 - david
    
    Run Code Online (Sandbox Code Playgroud)

我怎么能建立一个linq查询,告诉我有多少人去参加各方,结果是:

party 1 , 2 people
party 2 , 1 people
party 3 , 2 people
Run Code Online (Sandbox Code Playgroud)

L.B*_*L.B 5

list.GroupBy(x => x.Party)
    .Select(x => new { Party = x.Key, Count = x.Count() });
Run Code Online (Sandbox Code Playgroud)