Thomas Krantz
Sep 8, 2009
  3057
(0 votes)

Filter and sort pages in as few lines as possible

An example of a couple of useful lambda expressions. At least I think so. These lines are used on a typical A-Z index page, and use a HTTP parameter (line 2) to filter the pages. On line 3 it uses a gorgious lambda to sort by page name.

Can it be done shorter or more beautiful, I wonder? :)

 
   1:  List<PageData> pages = new List<PageData>(GetChildren(CurrentPage.PageLink));
   2:  pages.RemoveAll(page => !page.PageName.ToUpper().StartsWith(Request["c"].ToUpper()));
   3:  pages.Sort((p1, p2) => p1.PageName.CompareTo(p2.PageName));
   4:   
   5:  // plDictionary is a PageList. Contains all pages with a page name starting with 
   6:  // the string Request["c"].
   7:  plDictionary.DataSource = pages;
   8:  plDictionary.DataBind();
Sep 08, 2009

Comments

Sep 21, 2010 10:32 AM

plDictionary.DataSource = GetChildren(CurrentPage.PageLink)
.AsEnumerable()
.Where(pd => string.Compare(pd.PageName, Request["c"], true) == 0)
.OrderBy(pd => pd.PageName);

/ Aanund Austrheim

Aanund Austrheim
Aanund Austrheim Sep 21, 2010 10:32 AM

Woops, wrong Where clause, just wrote it like I usually do:P Just switch it.

Thomas Krantz
Thomas Krantz Sep 21, 2010 10:32 AM

Yes, that was undoubtedly awesome :)

Magnus Rahl
Magnus Rahl Sep 21, 2010 10:32 AM

Extensions + lambda rocks!

Sep 21, 2010 10:32 AM

Great stuff! Straight into the favourites :)
/ Martin Söderlund

Please login to comment.
Latest blogs
Extended SelectionFactory

In one of my previous articles I described new Color Picker widget. Today I’d like to show another simple property. I prepared extended version of...

Grzegorz Wiecheć | Jun 7, 2023 | Syndicated blog

Unlock Creativity with Text Generation

Need fresh and innovative content ideas? The Epicweb.Optimizely.AIAssistant Addon has got you covered with its text generation feature. In this blo...

Luc Gosso (MVP) | Jun 6, 2023 | Syndicated blog

Breaking Language Barriers with Multilingual Text Translation

In today's globalized world, effective communication across languages is essential. The Epicweb.Optimizely.AIAssistant Addon makes language...

Luc Gosso (MVP) | Jun 6, 2023 | Syndicated blog

Optimizely Search & Navigation - Get autocomplete suggestions in right language

When you are using Optimizely Search & Navigation (Find) to help you generate autocomplete suggestions server side in a multi-language scenario it...

Allan Thraen | Jun 5, 2023 | Syndicated blog