title: Identifying Unique Values in a C# List
body: <p>I have created a class, as below, to represent a Composite Primary Key model:</p>
  
  <pre><code>public class PrimaryKeyModel
  {
      public string ColumnName { get; set; }
      public string ColumnValue { get; set; }
      public int RowNumber { get; set; } // always unique
  }
  </code></pre>
  
  <p>It basically represents the names/values of the columns which together make up the primary key, plus the row number where this combination belongs; originally in a Spreadsheet.</p>
  
  <p>I have then put this model in a List and populated it with data from a spreadsheet: </p>
  
  <pre><code>List&lt;PrimaryKeyModel&gt; primaryKeysList = new List&lt;PrimaryKeyModel&gt;; 
  </code></pre>
  
  <p>I would like to check primaryKeysList and see if it has any duplicated values, and if it has, I would like to know the Row numbers where these values are duplicated. </p>
  
  <p>I have tried different ways such as loading this list into a HashSet, a dictionary and to use <a href="https://stackoverflow.com/questions/6148809/how-to-linq-distinct-by-multiple-fields-without-anonymous-types">this solution here at this link</a> but non of it worked. Is there anyway I could resolve this.</p>
  
  <p>Thanks. </p>
  
  <p><strong>Update - Here is a sample data display. UniqueColumnsModel is the same as PrimaryKeyModel; I have changed it here to make it clearer.</strong></p>
  
  <p><img src="https://i.stack.imgur.com/MfsZ5.png" alt="enter image description here"></p>
  
  <p><strong>Edit: Clarification of the question</strong></p>
  
  <p>I am trying to import data from a spreadsheet (which can have many types(one for sales, one for quotes ..etc.)) into a database. A configuration table in the database determines which column(s) in a spreadsheet will constitute the primary key in the destination table. My task is to create a routine which validate spreadsheet data before being it being uploaded (imported) into the database using my application. I want ot validate that the columns set as the composites of the primary key, do not contain any duplicated data, so that the primary key constraint is NOT violated in the destination table on insert.. </p>
  
  <p>The list mentioned here (PrimaryKeyModel) contains the name of the column in the spreadsheet (which together with others constitutes the primary key), the value of the column in the spreadsheet and the row number in the spreadsheet where this value exists. The list gets populated via a foreach row/ foreach column loops. So I hope this elaborates the question better.</p>