how to ascending based on child class.

Posted by Jayakumars under ASP.NET MVC on 3/17/2014 | Points: 10 | Views : 640 | Status : [Member] [MVP] | Replies : 1
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


Responses

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