Implement Sql Server Rank & Denserank Functions in 'LINQ Expressions'

Posted by kgovindarao523-21772 under C# category on | Points: 40 | Views : 4306

The Objective of code is:

In a class there are 100 students, and in talent test they got some marks. I want to rank those students. To implement this,
In Sql Server there are two functions called Rank and Denserank .
Here i implemented those functions in C# using Linq expressions .

In data base, My table contains Three columns
and i used entity data model(EDMX)

#region Rank 
var RestTable = from o in fmodel.tableToRankStudentses
select o;
var limitedUsers = from o in fmodel.tableToRankStudentses
orderby o.Marks descending
select new
Name = (from n in fmodel.tableToRankStudentses
where n.Marks == o.Marks & n.Id == o.Id
select n.Name).FirstOrDefault(),
TotalSales = o.Marks,
Position = RestTable.Count(s2 => s2.Marks > o.Marks) + 1

GrdRank.DataSource = limitedUsers.ToList();

#region DenseRank
var limitedUsers = from o in fmodel.tableToRankStudentses
group o by new { o.Marks, o.Id }
into g
orderby g.Key.Marks descending
select new
Name =(from o in fmodel.tableToRankStudentses
where o.Marks == g.Key.Marks && o.Id == g.Key.Id
select o.Name).FirstOrDefault(),
TotalSalary = g.Key.Marks,
Position = (from o in fmodel.tableToRankStudentses
group o by o.Marks into l
select l).Count(s => s.Key > g.Key.Marks) + 1


Thank you,

Comments or Responses

Login to post response