您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“怎么用lambda表达式进行对象结合操作”,在日常操作中,相信很多人在怎么用lambda表达式进行对象结合操作问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用lambda表达式进行对象结合操作”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
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 }
上面是测试需要的简单类型: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 }
到此,关于“怎么用lambda表达式进行对象结合操作”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。