你如何使用LINQ查找特定属性的重复? [英] How do you use LINQ to find the duplicate of a specific property?

查看:97
本文介绍了你如何使用LINQ查找特定属性的重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 客户customerOne =新客户(约翰福音,李四); 
客户customerTwo =新客户(超级,人);
客户customerThree =新客户(疯狂的,人);
客户customerFour =新客户(简,李四);
客户customerFive =新客户(蝙蝠,人);

名单,LT;客户>客户=新的List<客户>();
customers.Add(customerOne);
customers.Add(customerTwo);
customers.Add(customerThree);
customers.Add(customerFour);
customers.Add(customerFive);

什么LINQ查询将返回一个枚举为所有客户提供相同的姓氏?



结果应该包括的一个实例:李四,李四,超男子,和蝙蝠男


解决方案

  VAR的结果=从C在客户
在c.LastName客户携手C2等于c2.LastName
,其中c!= C2
选择C;


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

What LINQ query would return an enumerable for all customers with the same last name?

Results should include one instance of: John Doe, Jane Doe, Super Man, and Bat Man

解决方案

    var result = from c in customers
                 join c2 in customers on c.LastName equals c2.LastName
                 where c != c2
                 select c;

这篇关于你如何使用LINQ查找特定属性的重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆