POIR
POIR

Reputation: 3190

Use Linq to move item to bottom of list

This is my code:

var list = from c in child_categories orderby c.Translated_Syntax 
           select new { Name = c.Translated_Syntax, ID = c.ID_Category } ;

lst_category.DataSource = list;    
lst_category.DataBind();

My list of categories is: Car, Other, Children, House, Girl, Show.

If I order my list, the result it will be:

But I want the 'Other' item to be placed to bottom of list. Something like this:

How can I do this?

Upvotes: 18

Views: 6264

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460158

You can use this Orderby-"trick":

from c in child_categories 
orderby c.Translated_Syntax == "Other", c.Translated_Syntax

You can remember how it works in this way: the comparison returns a bool where true is 1 and false is 0. That's why orderby Translated_Syntax == "Other" will list the "Others" last. If you want it to precede you either have to reverse the condition or (better) use descending instead.

Upvotes: 33

Related Questions