Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ to SQL omit field from results while still including it in the where clause
    text
    copied!<p>Basically I'm trying to do this in LINQ to SQL;</p> <pre><code>SELECT DISTINCT a,b,c FROM table WHERE z=35 </code></pre> <p>I have tried this, (c# code)</p> <pre><code>(from record in db.table select new table { a = record.a, b = record.b, c = record.c }).Where(record =&gt; record.z.Equals(35)).Distinct(); </code></pre> <p>But when I remove column z from the table object in that fashion I get the following exception;</p> <blockquote> <p>Binding error: Member 'table.z' not found in projection.</p> </blockquote> <p>I can't return field z because it will render my distinct useless. Any help is appreciated, thanks.</p> <p><strong>Edit:</strong> </p> <p>This is a more comprehensive example that includes the use of PredicateBuilder,</p> <pre><code>var clause = PredicateBuilder.False&lt;User&gt;(); clause = clause.Or(user =&gt; user.z.Equals(35)); foreach (int i in IntegerList) { int tmp = i; clause = clause.Or(user =&gt; user.a.Equals(tmp)); } var results = (from u in db.Users select new User { a = user.a, b = user.b, c = user.c }).Where(clause).Distinct(); </code></pre> <p><strong>Edit2:</strong> </p> <p>Many thanks to everyone for the comments and answers, this is the solution I ended up with,</p> <pre><code>var clause = PredicateBuilder.False&lt;User&gt;(); clause = clause.Or(user =&gt; user.z.Equals(35)); foreach (int i in IntegerList) { int tmp = i; clause = clause.Or(user =&gt; user.a.Equals(tmp)); } var results = (from u in db.Users select u) .Where(clause) .Select(u =&gt; new User { a = user.a, b = user.b, c = user.c }).Distinct(); </code></pre> <p>The ordering of the Where followed by the Select is vital.</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