-1
bookingInfo.CreditCardList = airUserCreditCardList
  .Distinct()
  .OrderBy(o => o.Text)
  .ToList();

I am trying to order the list based on the value inside the Text property.Is there a way I can order the list only if the Object's property Text has value "Primary".

My list looks like this


     Text="abc",Code="123"----0th index
        Text ="Select",Code="1233"--1st Index
        Text="Primary",Code="0000"--2ndIndex```

But i want it to look like this only if the Primary card is present in the list 

    ```
Text="Primary",Code="0000"--0th Index
    Text ="Select",Code="1233"--1st Index
        Text ="abc",Code="123"--2nd Index

rock rake
  • 5
  • 1
  • 6
    Can you give some examples illustrating what you mean by "order the list only if the value of Text is primary else no ordering should occur"? – Sweeper Jul 01 '21 at 09:32
  • It's not clear, if you only want to order by records which text is "primary" you mean you want to see all "primary"-credit-cards first and then rest? – Tim Schmelter Jul 01 '21 at 09:38
  • 1
    It's unclear what is "if value of `Text` is primary". Something like this: `.OrderBy(o => o.Text != "primary")` – Dmitry Bychenko Jul 01 '21 at 09:39
  • Why "abc" comes after "Select" if "Primary" is in the list? I understand that "Primary" comes first but if you would order by `Text` the word "abc" would come before "Select". It's also not the original order, so it seems suddenly "abc" comes at the end for no reason. Explain the rules. – Tim Schmelter Jul 01 '21 at 10:01

1 Answers1

0

You can order by bool value (note, however, that false < true):

  bookingInfo.CreditCardList = airUserCreditCardList
    .Distinct()
    .OrderBy(o => !o.Text.Contains("Text=\"Primary\""))
    .ToList();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215