Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting rid of outer foreach loop using linq or lambda and ensuring no duplicates are added to list
    primarykey
    data
    text
    <p>I've got 2 parts to this question:</p> <ol> <li><p>Is there way to get rid of the outer loop</p> <pre><code>List&lt;Document&gt; newDocuments = new List&lt;Document&gt;(); foreach (DocumentDetail documentDetail in documentDetails) { newDocuments.AddRange(documents.FindAll(d =&gt; d.Extension.ToUpperInvariant() == documentDetail.Extension.ToUpperInvariant())); } </code></pre></li> <li><p>As you can see from the above, I'm just dealing with the "Extension" and even if I ended up keeping my outer foreach loop, I still want to check every other properties (i.e. description, etc...) so I may end up with multiple calls to the inner part and it will ended up looking like this, assuming I'm now checking Description</p> <pre><code>newDocuments.AddRange(documents.FindAll(d =&gt; d.Description.ToUpperInvariant() == documentDetail.Description.ToUpperInvariant())); </code></pre></li> </ol> <p>The problem with the above is that I could end up with duplicate documents if a document happens to have a .pdf extension and then matches the description. </p> <p>How can I ensure that no duplicates documents are added. Can I add something to my linq query (or to lambda?). In terms of uniqueness, while not definite, for now I have access to a "documentno" which is unique for all the documents held in the "documents" lists.</p> <p>If you know the answer to one part or the other, please let me know.</p> <p>Appreciated!</p> <h1>Edited</h1> <p>What about this? It definitely works, but I'm not sure this is the right way to write this?</p> <pre><code>List&lt;Document&gt; newDocs = (from documentDetail in documentDetails from document in documents where document.Extension.ToUpperInvariant() == documentDetail.Extension.ToUpperInvariant() select document).Distinct().ToList(); </code></pre> <p>Am I better off just sticking to foreach loop?</p> <p>Using the above, I would end up with something like this if I wanted to check multiple properties from the DocumentDetails list against my Document List:</p> <pre><code> List&lt;Document&gt; newDocuments = null; if (documentDetails.FindAll(dd =&gt; (dd.Extension != null || !string.IsNullOrEmpty(dd.Extension))).Count &gt; 0) { newDocuments = (from documentDetail in documentDetails from document in documents where document.Extension.ToUpperInvariant() == documentDetail.Extension.ToUpperInvariant() select document).Distinct().ToList(); } if (documentDetails.FindAll(dd =&gt; (dd.Description != null || !string.IsNullOrEmpty(dd.Description))).Count &gt; 0) { newDocuments = (from documentDetail in documentDetails from document in documents where document.Description.ToUpperInvariant() == documentDetail.Description.ToUpperInvariant() select document).Distinct().ToList(); } </code></pre> <p>While I'd still like someone to confirm that the way I wrote this is correct, I'm still left with how to append the results and then remove all duplicates. I guess when all is done, I could apply the Distinct, but I'm still left with "appending" issue. How do I do this then?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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