Note that there are some explanatory texts on larger screens.

plurals
  1. POFilter and keep first object of a List of objects with properties that match
    text
    copied!<p>I apologize upfront, because I now realize that I have completely worded my example wrong. For those who have given responses, I truly appreciate it. Please let me re-attempt to explain with a more accurate details. Please edit your responses, and once again, I apologize for not being more exact in my previous posting.</p> <p>Using an entity framework model class called Staging (which is a representation of my Staging table), I have the following <code>List&lt;Staging&gt;</code>.</p> <pre><code>List&lt;Staging&gt; data = (from t in database.Stagings select t).ToList(); //check for an empty List...react accordingly... </code></pre> <p>Here is a quick look at what <code>Staging</code> looks like:</p> <pre><code>public partial class Staging { public int ID { get; set; } //PK public int RequestID { get; set; } //FK ... public string Project { get; set; } ... } </code></pre> <p>Let us suppose that the query returns 10 records into my <code>data</code> list. Let us also suppose that <code>data[3]</code>, <code>data[6]</code>, and <code>data[7]</code> each have the same value in <code>data.Project</code>, let's say <code>"Foo"</code>. The <code>data.Project</code> value is not known until runtime.</p> <p>Given this, how would I keep the first occurrence, <code>data[3]</code>, and remove <code>data[6]</code> and <code>data[7]</code> from my <code>List&lt;Staging&gt;</code>?</p> <p><strong>Edit:</strong> I have the following code that works, but is there another way?</p> <pre><code> HashSet&lt;string&gt; projectValuesFound = new HashSet&lt;string&gt;(); List&lt;Staging&gt; newData = new List&lt;Staging&gt;(); foreach (Staging entry in data) { if (!projectValuesFound.Contains(entry.Project)) { projectValuesFound.Add(entry.Project); newData.Add(entry); } } </code></pre>
 

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