Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
SQL Server 119
#SQLServer 88
Oracle 70
#SQL SERVER 35
BRH 31
SQL Server 2012 29
denali 23
#TSQL 19
TSQL 19
C# 15

Archive · View All
October 2011 31
November 2011 30
September 2011 30
August 2011 18
December 2011 15
July 2011 13
June 2011 8
May 2012 4
April 2012 3
January 2010 3

Linq query to find the N-th highest salary of employee.The program should handle ties also

Jul 31 2012 12:00AM by Niladri Biswas   

Let us first make the environment.First let us declare an Employee class

class Employee
{
	public string EmpName { get; set; }
	public int Salary { get; set; }
}

Then populate some records to it

private static List<employee> GetEmpRecord()
{
	List<employee> empCollection = new List<employee>();
	empCollection.Add(new Employee { EmpName = "Emp1", Salary = 10000 });
	empCollection.Add(new Employee { EmpName = "Emp2", Salary = 3456 });
	empCollection.Add(new Employee { EmpName = "Emp3", Salary = 14256 });
	empCollection.Add(new Employee { EmpName = "Emp4", Salary = 15000 });
	empCollection.Add(new Employee { EmpName = "Emp5", Salary = 10000 });
	empCollection.Add(new Employee { EmpName = "Emp6", Salary = 6000 });
	empCollection.Add(new Employee { EmpName = "Emp7", Salary = 2000 });
	empCollection.Add(new Employee { EmpName = "Emp8", Salary = 5000 });
	empCollection.Add(new Employee { EmpName = "Emp9", Salary = 7000 });
	empCollection.Add(new Employee { EmpName = "Emp10", Salary = 7000 });
	return empCollection;
} 

Finally let us write the program

var empCollection = GetEmpRecord();           

var employees = (from emp in empCollection
				 group emp by emp.Salary into g
				 orderby g.Key descending 
				 select new
					 {
						EmpRecord = g.ToList()       
					 }).ToList();

Let us analyze the program. First we are grouping up the Employee Record Set by their salary.

var employees = (from emp in empCollection
				 group emp by emp.Salary into g                            
				 select new
				 {
					EmpRecord = g.ToList()       
				 }).ToList();

If we put a break point there and check , we will find that there are a total of 8 elements since both Emp1 and Emp5 are earning 10000 while Emp9 and Emp10 are earning 7000 each.But it is not sorted. Inorder to sort the record set, we have introduce the order by clause that will guarenteed to bring teh result in a sorted order.

Finally we are projecting the record using the select statement.At this stage we will get the count as 8 only but with sorted record set.

Suppose we want to find out the third highest salary which should be "Emp1" and "Emp5" (both earning 10000 each). The first will be "Emp4" with 15000 and second is "Emp3" with Salary = 14256.

Since it is a collection, we can use index as shown under to obtain the desired result.

 var result = employees[2];

will give the desire result.

The final program will be

 
var empCollection = GetEmpRecord();
int whichEmpSalary = 3;

var employees = (from emp in empCollection
				 group emp by emp.Salary into g
				 orderby g.Key descending 
				 select new
					 {
						 EmpRecord = g.ToList()                                    
					 }).ToList();

employees[whichEmpSalary - 1].EmpRecord
	.ForEach(i => Console.WriteLine("Emp Name {0} earns {1}", i.EmpName, i.Salary));

Console.ReadKey(); 

Output
Emp Name Emp1 earns 10000
Emp Name Emp5 earns 10000

A lambda version will be as under

//Lambda version
empCollection
	.GroupBy(g=>g.Salary)
	.OrderByDescending(o=>o.Key)
	.Select(s=>new{EmpRecord = s.ToList()})
	.ToList()[whichEmpSalary - 1].EmpRecord
	.ForEach(i => Console.WriteLine("Emp Name {0} earns {1}", i.EmpName, i.Salary));

Tags: Linq


Niladri Biswas
7 · 21% · 6720
4
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Linq query to find the N-th highest salary of employee.The program should handle ties also" rated 5 out of 5 by 4 readers
Linq query to find the N-th highest salary of employee.The program should handle ties also , 5.0 out of 5 based on 4 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]