复杂的 LINQ 查询

发布于 2024-10-19 10:48:03 字数 515 浏览 2 评论 0原文

考虑集合

List<Person> people = new List<Person>
        {
            new Person{Name = "A", SSN="1", Age = 23},
            new Person{Name = "A", SSN="2", Age = 23},
            new Person{Name = "B", SSN="3", Age = 24},
            new Person{Name = "C", SSN="4", Age = 24},
            new Person{Name = "D", SSN="5", Age = 23}
        };

问题是:如何编写一个 LINQ 查询来按年龄对人员进行分组,然后计算每个组中具有相同名称的人员数量?

我尝试使用 group by 运算符、嵌套查询所有可能性仍然无法找出确切的查询。

问候, 哎呀

Consider the collection

List<Person> people = new List<Person>
        {
            new Person{Name = "A", SSN="1", Age = 23},
            new Person{Name = "A", SSN="2", Age = 23},
            new Person{Name = "B", SSN="3", Age = 24},
            new Person{Name = "C", SSN="4", Age = 24},
            new Person{Name = "D", SSN="5", Age = 23}
        };

The question is: How can I write a LINQ query to group Person on Age and then count number of person with in each group having the same name?

I tried using group by operator, nested queries all possibilities still could not figure out the exact query.

Regards,
Jeez

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

庆幸我还是我 2024-10-26 10:48:03

var ages  = from p in people 
            group p by p.Age into g
            select new { 
                Age = g.Key, 
                Count = g.Count(), 
                Names = from prs in g 
                        group prs by prs.Name
            };

Names 属性将名称作为 Key,然后您可以获取每个名称的 Count()

How about

var ages  = from p in people 
            group p by p.Age into g
            select new { 
                Age = g.Key, 
                Count = g.Count(), 
                Names = from prs in g 
                        group prs by prs.Name
            };

The Names property will have the name as the Key and you can then get Count() for each name.

少钕鈤記 2024-10-26 10:48:03

基本上与:

var peeps = 
    people.GroupBy(p => p.Age).Select(gp => new
          {
              Aged = gp.Key,
              Count = gp.Count(),
              NameGrp = gp.GroupBy(pers => pers.Name)
          });

吉姆相同

basically the same as:

var peeps = 
    people.GroupBy(p => p.Age).Select(gp => new
          {
              Aged = gp.Key,
              Count = gp.Count(),
              NameGrp = gp.GroupBy(pers => pers.Name)
          });

jim

自在安然 2024-10-26 10:48:03

这与按年龄和姓名分组没有什么不同,包括计数以及可选地按年龄和姓名排序。对对的 LINQ 操作通常可以通过简单地使用匿名类型来完成,如下所示:

var people = new List<Person> { 
    new Person { FirstName = "John", LastName = "Leidegren", Age = 25 },
    new Person { FirstName = "John", LastName = "Doe", Age = 25 },
    new Person { FirstName = "Not john", LastName = "Doe", Age = 26 },
};

foreach (var g in (from p in people
                   group p by new { p.Age, p.FirstName }))
{
    Console.WriteLine("Count: {0}", g.Count());

    foreach (var person in g.OrderBy(x => x.FirstName))
    {
        Console.WriteLine("FirstName: {0}, LastName: {1}, Age: {2}"
            , person.FirstName
            , person.LastName
            , person.Age);
    }
}

这里的唯一键非常清楚,并且可以更改它,而无需更改其余程序的结构。

This is no different from grouping by both age and name, including count and optionally order by age and name. LINQ operations on pairs can usually be done by simply using anonymous types like so:

var people = new List<Person> { 
    new Person { FirstName = "John", LastName = "Leidegren", Age = 25 },
    new Person { FirstName = "John", LastName = "Doe", Age = 25 },
    new Person { FirstName = "Not john", LastName = "Doe", Age = 26 },
};

foreach (var g in (from p in people
                   group p by new { p.Age, p.FirstName }))
{
    Console.WriteLine("Count: {0}", g.Count());

    foreach (var person in g.OrderBy(x => x.FirstName))
    {
        Console.WriteLine("FirstName: {0}, LastName: {1}, Age: {2}"
            , person.FirstName
            , person.LastName
            , person.Age);
    }
}

It's very clear what the unique key is here and it can be changed without having to also change the structure of the remaining program.

不甘平庸 2024-10-26 10:48:03
List<Person> persons = new List<Person>
{
   new Person{Name = "A", SSN="1", Age = 23},
   new Person{Name = "A", SSN="2", Age = 23},
   new Person{Name = "B", SSN="3", Age = 24},
   new Person{Name = "C", SSN="4", Age = 24},
   new Person{Name = "D", SSN="5", Age = 23}
};
var out1 = persons.GroupBy(p => new { p.Age, p.Name }).Select(s => new { Name = s.Key.Name,age = s.Key.Age, cnt = s.Count() }); ;
System.Threading.Tasks.Parallel.ForEach(out1, item => Console.WriteLine(item.Name + " " + item.age + " " + item.cnt));  
List<Person> persons = new List<Person>
{
   new Person{Name = "A", SSN="1", Age = 23},
   new Person{Name = "A", SSN="2", Age = 23},
   new Person{Name = "B", SSN="3", Age = 24},
   new Person{Name = "C", SSN="4", Age = 24},
   new Person{Name = "D", SSN="5", Age = 23}
};
var out1 = persons.GroupBy(p => new { p.Age, p.Name }).Select(s => new { Name = s.Key.Name,age = s.Key.Age, cnt = s.Count() }); ;
System.Threading.Tasks.Parallel.ForEach(out1, item => Console.WriteLine(item.Name + " " + item.age + " " + item.cnt));  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文