Three ways to remove weight from C # list

Source: Internet
Author: User

Reprint to: 46839793

Three ways to de-weigh

1. The elements in the list implement the Iequatabe interface and provide the Equals method and the GetHashCode method.

2. Using expressions

Users. Where ((x,i) =>users. FindIndex (Z=>z.name = = x.name) = = i)

Go heavy, this statement returns the result only the first of the duplicate elements in the list (name equals is considered duplicates).

3. Use loops to determine if each element repeats

[CSharp]View PlainCopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Namespace Nonduplicatelist
  5. {
  6. Class Program
  7. {
  8. static void Main (string[] args)
  9. {
  10. list<user> users = new list<user> ();
  11. Users.  ADD (new User ("Zhang San", "Yongfeng road No. 299"));
  12. Users. ADD (new User ("Zhang San", "8th on West Road")); Duplicates, which will be deleted when the weight is removed.
  13. Users.  ADD (new User ("John Doe", "Sea Eagle Road Armour One"));
  14. List<user> nonDuplicateList1 = users. Distinct (). ToList (); //Through the Equals of the user class to achieve the weight
  15. List<user> nonDuplicateList2 = users. Where ((x,i) =>users. FindIndex (Z=>z.name = = x.name) = = i). ToList (); //lambda expression de-weight
  16. list<user> nonDuplicateList3 = new list<user> (); To weigh in a circular way
  17. foreach (user user in users)
  18. {
  19. if (nonduplicatelist3.exists (x=>x.name==user.name) = = false)
  20. {
  21. Nonduplicatelist3.add (user);
  22. }
  23. }
  24. foreach (list<user> List in new OBJECT[]{NONDUPLICATELIST1,NONDUPLICATELIST2,NONDUPLICATELIST3}) //Print out three elements of list
  25. {
  26. Console.Write ("nonduplicatelist:\r\n");
  27. foreach (User u in list)
  28. {
  29. Console.WriteLine ("\ T" + u.tostring ());
  30. }
  31. }
  32. Console.read ();
  33. }
  34. }
  35. class user:iequatable<user>//inherits the IEquatable interface, implementing the Equals method. List can use distinct to go to heavy
  36. {
  37. public string name { get;  set;}
  38. string Address;
  39. Public User (string _name, string _address)
  40. {
  41. name = _name;
  42. address = _address;
  43. }
  44. public Override string ToString ()
  45. {
  46. return string.  Format ("Name:{0},\taddress:{1}", name, address);
  47. }
  48. Public bool Equals (User other)
  49. {
  50. return this.name = = Other.name;
  51. }
  52. public override int GetHashCode ()
  53. {
  54. return name.  GetHashCode ();
  55. }
  56. }
  57. }

Three ways to remove weight from C # list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.