title: Implementing RANK OVER SQL Clause in C# LINQ
body: <p>I need to implement the following T-SQL clause ....</p>
  
  <pre><code>RANK() OVER (PARTITION BY a.CategoryKey ORDER BY (x.Rate * @BASE_RATE ) DESC )as Rank
  </code></pre>
  
  <p>...in C# LINQ. So far what I've come up with is something like ....</p>
  
  <pre><code>var rank = data.GroupBy(d =&gt; d.CategoryKey)
                 .Select(group =&gt; group.OrderByDescending(g =&gt; g.Rate * @BAES_RATE) 
  </code></pre>
  
  <p>I <em>think</em> this would give me each rank partition ordered by rate * BASE_RATE. But what I actually need is the <em>individual rank</em> of a single row, with this being a subquery within a larger result. So really the full SQL query I'm working from is something like ....</p>
  
  <pre><code>SELECT 
      a.Rate,
      a.CategoryKey,
      a.ID,
      .
      .
      .
      RANK() OVER (PARTITION BY a.CategoryKey ORDER BY (x.Rate * @BASE_RATE ) DESC )as Rank
  FROM data
  </code></pre>