Home > Backend Development > C#.Net Tutorial > Detailed explanation of examples of object combination operations using lambda expressions

Detailed explanation of examples of object combination operations using lambda expressions

零下一度
Release: 2017-06-28 15:46:40
Original
1808 people have browsed it

1publicclass Person : BaseDomain
 2    {
 3long _id;
 4string firstName;
 5string secondName;
 6string comments;
 7 8public Person()
 9        {}
1011public Person(long id)
12        {
13this._id = id;
14        }
15public Person(long id,string firstName, string secondName)
16        {
17this._id = id;
18this.firstName = firstName;
19this.secondName = secondName;
20             comments = String.Empty;
21        }
22public Person(long id,string firstName, string secondName, string comments)
23             : this(id,firstName, secondName)
24        {
25this.comments = comments;
26        }
2728publicstring FirstName
29        {
30get { return firstName; }
31set { firstName = value; }
32        }
33publicstring SecondName
34        {
35get { return secondName; }
36set { secondName = value; }
37        }
38publicstring Comments
39        {
40get { return comments; }
41set { comments = value; }
42        }
43publicoverridestring ToString()
44        {
45returnstring.Format("FirstName: {0}\tSecondName: {1}\tComment: {2}", this.firstName, this.secondName, this.comments);
46        }
47     }
Copy after login

View Code

The above is the simple type required for testing: Person

1var 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; " 9var result_list = list.GroupBy(obj=>obj.FirstName).Select(g=>g.First()).ToList();
1011foreach(var item in result_list)
12    {
13        Console.WriteLine(item);
14     }
Copy after login

View Code

The above introduces the use of Lambda expressions Lambda expressions perform object combination filtering operations, including the content of Lambda expressions. I hope it will be helpful to friends who are interested in .NET tutorials.


The above is the detailed content of Detailed explanation of examples of object combination operations using lambda expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template