title: How can I check if an object (that I have to insert in a list) is already in the list using C#?
body: <p>I am not so into <strong>C#</strong> and <strong>.NET</strong>. I have the following problem.</p>
  
  <p>In my code I have something like this:</p>
  
  <pre><code>UOR uor;
  
  foreach (int idUor in visibilitaPostRidistribuzioni)
  {
      uor = UorSQL.GetUorFromId(siglaAOO, idUor, dbConfig);
  
      visibUtils.Uors.Add(uor);
  }
  </code></pre>
  
  <p>where <strong>visibUtils.Uors</strong> is a list of <strong>UOR</strong> object.</p>
  
  <p>As you can see I am iterating on a list of ID (<strong>visibilitaPostRidistribuzioni</strong>). For each ID I am retrieving an <strong>UOR uor</strong> object and I am adding it to the <strong>visibUtils.Uors</strong> list.</p>
  
  <p>What I need is a smart way to check if this <strong>uor</strong> object exist in the <strong>visibUtils.Uors</strong> list before insert it to avoid duplication. </p>
  
  <p>2 UOR object are the same object if they have the same values for these 2 fields: <strong>SiglaAOO</strong> and <strong>SiglaUOR</strong></p>
  
  <p>An <strong>UOR</strong> object is a model class like this:</p>
  
  <pre><code>public class UOR
  {
      private string unitaOperativaResponsabile;
  
      public string UnitaOperativaResponsabile
      {
          get { return unitaOperativaResponsabile; }
          set { unitaOperativaResponsabile = value; }
      }
      private string areaOrganizzativaOmogenea;
  
      public string AreaOrganizzativaOmogenea
      {
          get { return areaOrganizzativaOmogenea; }
          set { areaOrganizzativaOmogenea = value; }
      }
      private string siglaAOO;
  
      public string SiglaAOO
      {
          get { return siglaAOO; }
          set { siglaAOO = value; }
      }
      private string siglaUOR;
  
      public string SiglaUOR
      {
          get { return siglaUOR; }
          set { siglaUOR = value; }
      }
      private int idUor;
  
      public int IdUor
      {
          get { return idUor; }
          set { idUor = value; }
      }
  
      private bool attiva;
  
      public bool Attiva
      {
          get
          {
              return attiva;
          }
  
          set
          {
              attiva = value;
          }
      }
  
      private int alias;
  
      public int Alias
      {
          get
          {
              return alias;
          }
  
          set
          {
              alias = value;
          }
      }
  
      public override int GetHashCode()
      {
  
          // Get the hash code for the Textual field if it is not null.
          int hashTextual = siglaAOO == null ? 0 : siglaAOO.GetHashCode();
  
          // Get the hash code for the Digital field.
          int hashDigital = idUor.GetHashCode();
  
          // Calculate the hash code for the object.
          return hashDigital ^ hashTextual;
      }
  
      public override bool Equals(System.Object obj)
      {
          // If parameter is null return false.
          if (obj == null)
          {
              return false;
          }
  
          // If parameter cannot be cast to Point return false.
          UOR p = obj as UOR;
          if ((System.Object)p == null)
          {
              return false;
          }
  
          // Return true if the fields match:
          return (idUor == p.idUor) &amp;&amp; (siglaAOO.Equals(p.siglaAOO));
      }
  
      public bool Equals(UOR p)
      {
          // If parameter is null return false:
          if ((object)p == null)
          {
              return false;
          }
  
          // Return true if the fields match:
          return (idUor == p.idUor) &amp;&amp; (siglaAOO.Equals(p.siglaAOO));
      }
  }
  </code></pre>
  
  <p>I know that I can iterate on the <strong>visibUtils.Uors</strong> list and check if in this list exist an object having the same <strong>SiglaAOO</strong> and <strong>SiglaUOR</strong> fields value if the current object that I have to insert.</p>
  
  <p>But...exist some smarter way? (maybe something related to lambda expression)</p>