Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving duplicates from a sorted list c#
    text
    copied!<p>I have a list of details about a large number of files. This list contains the file ID, last modified date and the file path. The problem is there are duplicates of the files which are older versions and sometimes have different file paths. I want to only store the newest version of a file regardless of file path. So I created a loop that iterates through the ordered list, checks to see if the ID is unique and if it is, it gets stored in a new unique list. </p> <pre><code> var ordered = list.OrderBy(x =&gt; x.ID).ThenByDescending(x =&gt; x.LastModifiedDate); List&lt;Item&gt; unique = new List&lt;Item&gt;(); string curAssetId = null; foreach (Item result in ordered) { if (!result.ID.Equals(curAssetId)) { unique.Add(result); curAssetId = result.ID; } } </code></pre> <p>However this is still allowing duplicates into the DB and I can't figure out why this code isn't working as expected. By duplicates I mean, the files have the same ID but different file paths, which like I said before shouldn't be an issue. I just want the latest version regardless of pathway. Can anyone else see what the issue is? Thanks</p> <pre><code> var ordered = listOfItems.OrderBy(x =&gt; x.AssetID).ThenByDescending(x =&gt; x.LastModifiedDate); List&lt;Item&gt; uniqueItems = new List&lt;Item&gt;(); foreach (Item result in ordered) { if (!uniqueItems.Any(x =&gt; x.AssetID.Equals(result.AssetID))) { uniqueItems.Add(result); } } </code></pre> <p>this is what I have now and it is still allowing duplicates</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload