0

I'm trying to apply custom orderby logic over a list object. Currently I'm populating a dropdown for plan max and plan deductible using the list object and after applying order by this following value 0 comes as default for plan deductible. But I want to customize this order of plan deductible, instead of 0(zero) I want to display next highest value available (i.e 100 for this case). Here is the output Dropdown

I've have gone through this below link and applied this logic, but it is not working in my case Linq OrderBy custom order Custom Linq Ordering

Here is the code

int[] customOrder = { 100, 0, 50, 150, 250, 500, 1000, 2500, 5000, 10000, 50000 };
var Result = lstQuatationResult
    .OrderBy(x => x.planMaximum)
    .OrderBy(x => x.planDeductible)
    .OrderBy(x => Array.IndexOf(customOrder, x));

Thanks in advance.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    use `OrderBy(x => x.planMaximum).ThenBy(x => x.planDeductible).ThenBy(x => Array.IndexOf(customOrder, x));` – iSR5 Jun 06 '20 at 12:07
  • Every next `OrderBy` clause will reset previous ordering. – Guru Stron Jun 06 '20 at 12:07
  • Also can you give some examples of inputs and desired outputs. – Guru Stron Jun 06 '20 at 12:09
  • 1
    Does this answer your question? [LINQ OrderBy versus ThenBy](https://stackoverflow.com/questions/3760001/linq-orderby-versus-thenby) – Barns Jun 06 '20 at 13:03
  • Thanks everyone for your comment, but still the output is not as I am looking for. Currently I'm getting output for planDeductible like 0, 50, 100, 150, 250, 500 but the desired output is 100, 0, 50, 150, 250, 500. – user13219048 Jun 06 '20 at 16:08

1 Answers1

0

First of all every OrderBy clause will reset previous ordering (if actually any ordering will be performed by it). Use ThenBy method (or remove previous orderings):

var Result = lstQuatationResult
    .OrderBy(x => x.planMaximum)
    .ThenBy(x => x.planDeductible)
    .ThenBy(x => Array.IndexOf(customOrder, x)); // need to select some int field here

Also as I mentioned in the code - you need to select field of x in the last OrderBy clause because currently your Array.IndexOf is using IndexOf(Array array, object value) overload and tries to find an object in array of int's and returns -1 for all passed elements, basically keeping previous ordering:

Console.WriteLine(Array.IndexOf(customOrder, new object())); // -1

UPD

Try:

var Result = lstQuatationResult.OrderBy(x => Array.IndexOf(customOrder, x.planDeductible));

If this does not work for you please provide samples of lstQuatationResult and desired output.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • But still the output is not as follows for planDeductible like 0, 50, 100, 150, 250, 500 but the desired output is 100, 0, 50, 150, 250, 500. The custom filter is not working I guess. – user13219048 Jun 06 '20 at 16:10
  • @user13219048 remove all orderings except the last one(`.OrderBy(x => Array.IndexOf(customOrder, x.SomeField))`) and check. – Guru Stron Jun 06 '20 at 16:25