By MassStrike

2014-01-06 15:46:34 8 Comments

I need to implement the following T-SQL clause ....

RANK() OVER (PARTITION BY a.CategoryKey ORDER BY (x.Rate * @BASE_RATE ) DESC )as Rank C# LINQ. So far what I've come up with is something like ....

var rank = data.GroupBy(d => d.CategoryKey)
               .Select(group => group.OrderByDescending(g => g.Rate * @BAES_RATE) 

I think this would give me each rank partition ordered by rate * BASE_RATE. But what I actually need is the individual rank 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 ....

    RANK() OVER (PARTITION BY a.CategoryKey ORDER BY (x.Rate * @BASE_RATE ) DESC )as Rank
FROM data


@Adam Mills 2014-01-10 02:14:31

If you don't need the exact Rank semantics (i.e. tied ranks). You can use the index available with select projections

var rank = data.GroupBy(d => d.CategoryKey)
               .SelectMany(g => g.OrderByDescending(y => y.Rate * @BAES_RATE)              
                                 .Select((x,i) => new{g.Key, Item=x, Rank=i+1}))

Otherwise you can look at this answer

@MassStrike 2014-01-10 20:46:17

Cool thanks, I figured out the answer but this works too

@Frank Tzanabetis 2016-01-19 10:03:08

@MassStrike Care to share your implementation?

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] Why not inherit from List<T>?

40 Answered Questions

[SOLVED] Parameterize an SQL IN clause

17 Answered Questions

[SOLVED] SQL join: where clause vs. on clause

19 Answered Questions

[SOLVED] Why is Dictionary preferred over Hashtable in C#?

15 Answered Questions

[SOLVED] Error related to only_full_group_by when executing a query in MySql

9 Answered Questions

[SOLVED] Learning about LINQ

0 Answered Questions

T-SQL Select - Over clause equivalent LINQ Extension

4 Answered Questions

[SOLVED] Linq to SQL how to do "where [column] in (list of values)"

  • 2009-07-02 16:59:58
  • Nathan
  • 123068 View
  • 92 Score
  • 4 Answer
  • Tags:   linq linq-to-sql

2 Answered Questions

[SOLVED] SQL Rank in LINQ to Entities

  • 2015-05-05 18:46:57
  • madvora
  • 1677 View
  • 7 Score
  • 2 Answer
  • Tags:   c# linq

3 Answered Questions

[SOLVED] LINQ to SQL Where Clause Optional Criteria

Sponsored Content