Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Thomas Krantz
Sep 8, 2009
  3336
(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
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog