see my below code and guide me how to sort TotalCharge property in desending. thanks
staticclassProgram{/// <summary>/// The main entry point for the application./// </summary>[STAThread]staticvoidMain(){List<RateStatus> oRateCollection =newList<RateStatus>();RateStatus oRateStatus =newRateStatus();
oRateStatus.StatusMsg="Ok";
oRateStatus.ErrorMsg="None";
oRateStatus.RateDetails.Add(newRateDetails(){CurrentcyCode="GBP",TotalCharge=20});
oRateCollection.Add(oRateStatus);// i want something like this below//List<RateStatus> SortedList = objListOrder.OrderByDescending(o=>o.TotalCharge).ToList();}}publicclassRateStatus{publicRateStatus(){RateDetails=newList<RateDetails>();}publicstringStatusMsg{
get;set;}publicstringErrorMsg{
get;set;}publicList<RateDetails>RateDetails{
get;set;}}publicclassRateDetails{publicstringServiceCode{
get;set;}publicstringServiceName{
get;set;}publicstringCurrentcyCode{
get;set;}publicdoubleTotalCharge{
get;set;}}
No, instead you should be always explicit about the current blocking point.
So as you are ok with Linq, it seems you want to sort oRateCollection rather than, objListOrder. Also as the RateStatus doesn't have a TotalCharge property, my guess is that you want perhaps to sort on the sum of the total charges. It would give for example:
For now it seems you want to sort RateStatus on the "TotalCharge" but the problem is that this object doesn't have a TotalCharge field.
Instead they have 0 to n RateDetails so my guess is that you want to sort RateStatus on some value computed from the TotalCharge of its RateDetails (either the sum, max, min, average or whatever that would produce a single value for each RateStatus so that
it can be sorted).
Sum was just just a guess and seemed to make sense to me. Else try to explain in English on which value you want to sort your RateStatus (just TotalCharge won't work as RateStatus don't have this property).
Ah great. So you wanted to sort RateDetails for each RateStatus rather than to sort the RateStatus list.
As you can see, it's best to avoid just showing some code without any explanation. Instead try to always be explicit about the error you have or the thing you want to do so that those trying to help don't have to guess first why the wrong code which is shown
doesn't fit your need...
Participant
1347 Points
2399 Posts
How to Sort a list for a specific case c#
Oct 28, 2014 05:26 AM|mou_inn|LINK
here is a sample code where i want get sorted data on TotalCharge property in desending order like this way.
see my below code and guide me how to sort TotalCharge property in desending. thanks
All-Star
48280 Points
17982 Posts
Re: How to Sort a list for a specific case c#
Oct 28, 2014 05:36 AM|PatriceSc|LINK
Hi,
This is part of System.Linq. Add a reference and a using System.Linq as needed...
Participant
1347 Points
2399 Posts
Re: How to Sort a list for a specific case c#
Oct 28, 2014 07:30 AM|mou_inn|LINK
that was done. just copy my code and test at your end then you can see the problem.
All-Star
48280 Points
17982 Posts
Re: How to Sort a list for a specific case c#
Oct 28, 2014 07:57 AM|PatriceSc|LINK
No, instead you should be always explicit about the current blocking point.
So as you are ok with Linq, it seems you want to sort oRateCollection rather than, objListOrder. Also as the RateStatus doesn't have a TotalCharge property, my guess is that you want perhaps to sort on the sum of the total charges. It would give for example:
If this is still not what you need, please be explicit about your issue.
Participant
1347 Points
2399 Posts
Re: How to Sort a list for a specific case c#
Oct 28, 2014 08:57 AM|mou_inn|LINK
why should i use Sum() ??
All-Star
48280 Points
17982 Posts
Re: How to Sort a list for a specific case c#
Oct 28, 2014 09:41 AM|PatriceSc|LINK
For now it seems you want to sort RateStatus on the "TotalCharge" but the problem is that this object doesn't have a TotalCharge field.
Instead they have 0 to n RateDetails so my guess is that you want to sort RateStatus on some value computed from the TotalCharge of its RateDetails (either the sum, max, min, average or whatever that would produce a single value for each RateStatus so that it can be sorted).
Sum was just just a guess and seemed to make sense to me. Else try to explain in English on which value you want to sort your RateStatus (just TotalCharge won't work as RateStatus don't have this property).
Participant
1347 Points
2399 Posts
Re: How to Sort a list for a specific case c#
Oct 28, 2014 10:22 AM|mou_inn|LINK
This way it has been sorted.
thanks
All-Star
48280 Points
17982 Posts
Re: How to Sort a list for a specific case c#
Oct 28, 2014 10:39 AM|PatriceSc|LINK
Ah great. So you wanted to sort RateDetails for each RateStatus rather than to sort the RateStatus list.
As you can see, it's best to avoid just showing some code without any explanation. Instead try to always be explicit about the error you have or the thing you want to do so that those trying to help don't have to guess first why the wrong code which is shown doesn't fit your need...