您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用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,"昭君","李","適合打團戰(zhàn)"); 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表達式進行對象結合操作”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。