title: LINQ - filter duplicates with condition
body: <p>I'm developing an .NET application where I need to filter data from SQL Server 2008 depending on a condition. My data structure looks like this: </p>
  
  <p><img src="https://i.stack.imgur.com/6WTYd.png" alt="enter image description here"></p>
  
  <p>The result should be grouped and sorted by Key. By default, the row with Revision null should be returned for each Key, but if revisionParameter is set the row with the corresponding Revision number should be returned. If the parameter is two, the output should be:</p>
  
  <pre><code>abc     2     FALSE
  def    null   TRUE
  ghj     2     FALSE
  klm    null   TRUE
  </code></pre>
  
  <p>How can I accomplish this in LINQ? Thanks</p>
  
  <p>EDIT:
  Response to lazyberezovsky:
  Your LINQ expression rewritten as lambda:</p>
  
  <pre><code>partial void RuleEntriesByUserSaveId_PreprocessQuery(int? UserSaveId, ref IQueryable&lt;RuleEntry&gt; query)
  {
  query = query.Where(re =&gt; re.Revision == null || re.Revision == value)
               .GroupBy(re =&gt; re.Key)
               .Select(g =&gt; g.FirstOrDefault(x =&gt; x.Revision != null) ?? g.First());
  }
  </code></pre>