How to ascending this

Posted by Jayakumars under ASP.NET MVC on 3/14/2014 | Points: 10 | Views : 2910 | Status : [Member] [MVP] | Replies : 7
hi

I fetch the IEnumerable<Employee> here
i need how to ascending and descending based on my Employee collection in based on salary
my salary is numeric how to put linq query for this based on salary ascending and descending

in linq

Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com



Responses

Posted by: Sheonarayan on: 3/14/2014 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hi Jaya,

There is an article written by me covering frequently used LINQ extension method and ascending / descending are one of them.

Read this article http://www.dotnetfunda.com/articles/show/993/frequently-used-linq-extension-methods.

You have to do something like below to make it descending
var query1 = query1.OrderByDescending(sample => sample.Salary);
amd
var query1 = query1.OrderBy(sample => sample.Name);
to make it ascending.

Hope this helps.

Thanks


Regards,
Sheo Narayan
http://www.dotnetfunda.com

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/14/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,
You can also use the inbuilt Enumerable.OrderBy method to sort your record.
Check the below link for more details
http://msdn.microsoft.com/en-us/library/bb534966(v=vs.110).aspx


Sample Code:

Class:
 class Employee

{
public string Name { get; set; }
public int Salary { get; set; }
}


Sorting the Records:

 //Populate Data

Employee[] employeeDetails = { new Employee { Name="Barley", Salary=8000 },
new Employee { Name="Boots", Salary=4987 },
new Employee { Name="Whiskers", Salary=1090 } };
//Use the Ordre by to sort records based on salary
IEnumerable<Employee> query = employeeDetails.OrderBy(emp => emp.Salary);


You can find a sample working example in the below link
http://dotnetfiddle.net/r1VHY5


Thanks,
A2H
My Blog

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/14/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Just now noticed that you need solution to sort the records in descending order also.
You can use the Enumerable.OrderByDescending method for that.
Check the below link for more details
http://msdn.microsoft.com/en-us/library/bb534855(v=vs.110).aspx

Sample Code:

Employee[] employeeDetails = { new Employee { Name="Barley", Salary=8000 },
new Employee { Name="Boots", Salary=4987 },
new Employee { Name="Whiskers", Salary=1090 } };

IEnumerable<Employee> query = employeeDetails.OrderByDescending(e => e.Salary);


You can find a working sample demo in below link
http://dotnetfiddle.net/pYEXYz

Thanks,
A2H
My Blog

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 3/14/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer
http://msdn.microsoft.com/en-us/library/bb534966(v=vs.110).aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 3/14/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://aspguy.wordpress.com/2008/09/10/sorting-and-ordering-linq-results-with-string-field-names/

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 3/17/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
a2h and bandi

my code this

public class Employee
{
public string Name;
public decimal Salary;

public List<Emppay> EmpData;
}

public class Emppay
{
public decimal Pfamt;
}
here how to descending in based of pfamt

how to ascending and desending in sub class

Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/17/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,
Are you looking for a code to Sort the List which is inside the class. if that is the case then you can use the below code

Sorting the Records

//Sort the inner list using Orderby Method

Employee[] employeeDetails = {
new Employee { Name="Barley", Salary=8000,EmpData=objEmp.OrderBy(x=>x.Pfamt).ToList() },
new Employee { Name="Boots", Salary=4987 , EmpData=objEmp2.OrderBy(x=>x.Pfamt).ToList() },
new Employee { Name="Whiskers", Salary=1090, EmpData=objEmp3.OrderBy(x=>x.Pfamt).ToList() }
};


Above code will sort the inner list available inside the class employee. You can also use the OrderByDescending method to order the list in descending order.

Complete code is given below

Class:

//Populate Dummy Data

List<Emppay> objEmp = new List<Emppay>
{
new Emppay { Pfamt = Convert.ToDecimal("354.34") },
new Emppay { Pfamt = Convert.ToDecimal("123.34") },
new Emppay { Pfamt = Convert.ToDecimal("789.34") }
};

List<Emppay> objEmp2 = new List<Emppay>
{
new Emppay { Pfamt = Convert.ToDecimal("987.87") },
new Emppay { Pfamt = Convert.ToDecimal("345.34") },
new Emppay { Pfamt = Convert.ToDecimal("789.34") }
};
List<Emppay> objEmp3 = new List<Emppay>
{
new Emppay { Pfamt = Convert.ToDecimal("123.34") },
new Emppay { Pfamt = Convert.ToDecimal("345.34") },
new Emppay { Pfamt = Convert.ToDecimal("789.34") }
};

//Sort the inner list using Orderby Method
Employee[] employeeDetails = {
new Employee { Name="Barley", Salary=8000,EmpData=objEmp.OrderBy(x=>x.Pfamt).ToList() },
new Employee { Name="Boots", Salary=4987 , EmpData=objEmp2.OrderBy(x=>x.Pfamt).ToList() },
new Employee { Name="Whiskers", Salary=1090, EmpData=objEmp3.OrderBy(x=>x.Pfamt).ToList() }
};


public class Employee
{
public string Name { get; set; }
public int Salary { get; set; }
public List<Emppay> EmpData;
}
public class Emppay
{
public decimal Pfamt;
}


You can find a working sample demo in below link

http://dotnetfiddle.net/gMMjcy

Thanks,
A2H
My Blog

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response