Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

377
Views
¿Cómo convertir List<T> a HashSet<T> en C#?

Tengo una Lista que tiene duplicados de objetos. Para resolver eso, necesito convertir la Lista en un HashSet (en C#). ¿Alguien sabe cómo?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Asegúrese de que la clase de su objeto anule Equals y GetHashCode y luego puede pasar List<T> al constructor HashSet<T> .

 var hashSet = new HashSet<YourType>(yourList);

Puede ver: ¿Cuál es el mejor algoritmo para un System.Object.GetHashCode anulado?

over 4 years ago · Santiago Trujillo Report

0

Una forma alternativa sería

 var yourlist = new List<SomeClass>(); // [...] var uniqueObjs = yourlist.Distinct(); //Gives you a List with unique Objects of the List.

Tenga en cuenta que esto solo es posible si SomeClass anula GetHashCode y Equals de alguna manera. Esto también es cierto para

 var uniqueObjs = new HashSet<SomeType>(yourOriginalList);

De lo contrario, podría implementar su propia clase IEqualityComnparer y pasarla a distinto.

Tenga en cuenta que con el enfoque Distinct() , también puede buscar distintos valores de propiedad del objeto en la lista:

 var uniqueNames = yourlist.Select(obj => obj.Name).Distinct();

y algo más...

over 4 years ago · Santiago Trujillo Report

0

Si su tipo implementa IEquatable<T> , Equals() y GetHashCode() correctamente, entonces no necesita realizar la desduplicación usted mismo. Puede usar Distinct() de Linq para hacerlo así:

 myList = myList.Distinct().ToList();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!