gpt4 book ai didi

c# - 如何使用 LINQ 查找特定属性的副本?

转载 作者:IT王子 更新时间:2023-10-29 04:20:47 24 4
gpt4 key购买 nike

Customer customerOne = new Customer("John", "Doe");
Customer customerTwo = new Customer("Super", "Man");
Customer customerThree = new Customer("Crazy", "Guy");
Customer customerFour = new Customer("Jane", "Doe");
Customer customerFive = new Customer("Bat", "Man");

List<Customer> customers = new List<Customer>();
customers.Add(customerOne);
customers.Add(customerTwo);
customers.Add(customerThree);
customers.Add(customerFour);
customers.Add(customerFive);

什么 LINQ 查询会为具有相同姓氏的所有客户返回可枚举值?

结果应包括一个实例:John Doe、Jane Doe、Super Man 和 Bat Man

最佳答案

 customers.GroupBy(c => c.LastName).Where(g => g.Skip(1).Any()).SelectMany(c => c)

或使用 LINQ 语法:

        var q = from c in customers
group c by c.LastName into g
where g.Skip(1).Any()
from c in g
select c;

关于c# - 如何使用 LINQ 查找特定属性的副本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4250241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com