Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
my project was created using c# & entity framwork 6
i have this table (entries) that have status filed
the status filed will fill with one of this values :
Pending,Accepted,Rejected,Delayed,Completed

i want to retrieve all the record sorted with this conditions:-
Accepted rows first then Pending rows, then Delayed, and the completed and reject the last.

What I have tried:

var Query = DB1.entries.Where(u=> u.date >= From && u.date <= To && statusList.Contains( u.Status)).Select(s => new entires
            {
                ID = s.id,
                Name = s.Name,
                date = s.date,
                Order = s.order,
                Topic = s.Topic,
                ExternalVisitor = s.ExternalVisitor == 1 ? true : false,
                Reason = s.Reason,
                waiting = s.waiting,
                Status = s.Status,
               
            }).OrderBy(o => o.Status == "pending").ThenByDescending(o => o.date).ToList();
Posted
Updated 13-Sep-20 12:39pm

1 solution

C#
...OrderBy( o => 

switch( o.Status ) {
   case "Accepted": return 1;
   case "Pending": return 2;
   ...
   default: return 6;
} )
.ThenBy ... etc.
 
Share this answer
 
Comments
Golden Basim 14-Sep-20 1:26am    
i tried that but it seem that it don't accept to write switch inside orderby(). error line appear below the code.
Error CS1026 ) expected
Error CS1525 Invalid expression term 'switch'
Error CS0127 Since 'Entries.fillGrid()' returns void, a return keyword must not be followed by an object expression

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900