使用lambda表达式进行对象结合的筛选操作

 1 public class Person : BaseDomain
 2     {
 3         long _id;
 4         string firstName;
 5         string secondName;
 6         string comments;
 7 
 8         public Person()
 9         {}
10             
11         public Person(long id)
12         {
13             this._id = id;
14         }
15         public Person(long id,string firstName, string secondName)
16         {
17             this._id = id;
18             this.firstName = firstName;
19             this.secondName = secondName;
20             comments = String.Empty;
21         }
22         public Person(long id,string firstName, string secondName, string comments)
23             : this(id,firstName, secondName)
24         {
25             this.comments = comments;
26         }
27       
28         public string FirstName
29         {
30             get { return firstName; }
31             set { firstName = value; }
32         }
33         public string SecondName
34         {
35             get { return secondName; }
36             set { secondName = value; }
37         }
38         public string Comments
39         {
40             get { return comments; }
41             set { comments = value; }
42         }
43         public override string ToString()
44         {
45             return string.Format("FirstName: {0}	SecondName: {1}	Comment: {2}", this.firstName, this.secondName, this.comments);
46         }
47     }
View Code

上面是测试需要的简单类型:Person

 1     var list = new List<Person>(5);
 2     list.Add(new Person(1,"咬金","","拿斧子砍人的那个家伙");
 3     list.Add(new Person(2,"咬金","","拿斧子砍人的那个家伙");
 4     list.Add(new Person(3,"貂蝉","","3技能很厉害哦");
 5     list.Add(new Person(4,"昭君","","适合打团战");
 6     list.Add(new Person(5,"亚瑟","","狠狠厚的肉");
 7 
 8     //进行去重操作,需要先引入linq引用"using System.Linq; "
 9     var result_list = list.GroupBy(obj=>obj.FirstName).Select(g=>g.First()).ToList();
10 
11     foreach(var item in result_list)
12     {
13         Console.WriteLine(item);
14     }
View Code
原文地址:https://www.cnblogs.com/xakml/p/7017459.html