user2500796
user2500796

Reputation: 117

Linq query to sort collection

i have custom a collection LoanOptionsProgramVersionList which has bolean property Configured,string Description and Code.While insert and delete from the collection i want sort the collection in the below order. So one record inserted to database configured become yes. When deleted Configured become "No". So it Notify property.

  1. sort by configured
  2. then by description
  3. then by code

i have tried the below code After insert.

IOrderedEnumerable<ProgramVersionRecord> orderedList =    
     LoanOptionsProgramVersionList
         .OrderByDescending(p => p.ProgramVersionConfigured == true);

Also below code for deletion

 IOrderedEnumerable<ProgramVersionRecord> orderedList =
      LoanOptionsProgramVersionList.OrderBy(p => p.Description);

Any help will be appreciated.

Upvotes: 0

Views: 180

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

When ordering by ProgramVersionConfigured property use this property instead of result of comparison with boolean. Also use ThenBy to add another sorting:

 LoanOptionsProgramVersionList.OrderByDescending(p => p.ProgramVersionConfigured)
                              .ThenBy(p => p.Description)
                              .ThenBy(p => p.Code)

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460038

Use ThenBy

IOrderedEnumerable<ProgramVersionRecord> orderedList = LoanOptionsProgramVersionList
    .OrderByDescending(p => p.ProgramVersionConfigured) // configured has priority
    .ThenBy(p => p.Description)
    .ThenBy(p => p.Code);

Upvotes: 4

Related Questions