1

There are two Generic list of Inspectors and InspectorRates.

RateType has three different values (0 = Not Select ,1 = Day Rate ,2 = Hourly Rates).

I want to show all inspectors with Day Type Rate first and then lowest rate. If user selects option "Hourly Rates" then list needs to be sorted by Hourly Rate and thenlowest rate. Not seleted rate will always be at the bottom.

enter image description here

enter image description here

I have tried LINQ but its not working.

listI.OrderBy(Function(i) i.DefaultRate.RateType = Rates.RateTypeEnum.Day_Rate).ThenBy(Function(i) i.DefaultRate.Rate)
user1263981
  • 2,953
  • 8
  • 57
  • 98

1 Answers1

1

You can use OrderBy and ThenBy to provide priority-based search criteria

 List<Inspector> list = new List<Inspector>();

 list.Add(new Inspector() { RateType = 0, Rates = 0 });
 list.Add(new Inspector() { RateType = 0, Rates = -1 });
 list.Add(new Inspector() { RateType = 1, Rates = 1 });
 list.Add(new Inspector() { RateType = 1, Rates = -2 });
 list.Add(new Inspector() { RateType = 1, Rates = 3 });
 list.Add(new Inspector() { RateType = 2, Rates = 9 });
 list.Add(new Inspector() { RateType = 2, Rates = -2 });

 var sortedList = list
                   .OrderByDescending(i => i.RateType == 1)
                   .ThenBy(i => i.Rates).ToList();

output:

//RateType = 1, Rates = -2
//RateType = 1, Rates = 1
//RateType = 1, Rates = 3
//RateType = 2, Rates = -2
//RateType = 0, Rates = -1
//RateType = 0, Rates = 0
//RateType = 2, Rates = 9

Here is the Inspector class definition:

public class Inspector
{
    public int RateType { get; set; }
    public int Rates { get; set; }
    public int InspectorId { get; set; }

    public override string ToString()
    {
        return string.Format("Type:{0}, Rate:{1}", RateType, Rates);
    }
}
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
  • Side note: "this may help you" is not adding any information to the answer. Either have just code (which is ok) OR provide some description/links. For this particular question suggesting duplicate would be better approach as clearly it is not the first time one trying to sort list by 2 properties. – Alexei Levenkov Oct 03 '14 at 15:53
  • i am sorting by 2 properties but need to sort first one by certain value. – user1263981 Oct 03 '14 at 15:56
  • @user1263981 consider editing your question and make some numerical examples (inputs and desired outputs) to clarify what exactly you mean. – Hossein Narimani Rad Oct 03 '14 at 15:58
  • @HosseinNarimaniRad: your answer is very close to what i am looking for but i am getting error message "Data type(s) of the type parameter(s) in extension method 'Public Function OrderBy(Of TKey)(keySelector As System.Func(Of AMIS.Objects.Inspector, TKey)) As System.Linq.IOrderedEnumerable(Of AMIS.Objects.Inspector)' defined in 'System.Linq.Enumerable' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error." – user1263981 Oct 03 '14 at 16:08
  • Thats what i tried listI.OrderBy(Function(i) i >= i.DefaultRate.RateType.Day_Rate == 1).ThenBy(Function(i) i >= i.Rate) – user1263981 Oct 03 '14 at 16:08