Thomas Krantz
Sep 8, 2009
  3404
(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
Introducing Optimizely Graph Source .NET SDK

Overview Of Optimizely Graph Optimizely Graph is a cutting-edge, headless content management solution designed to integrate seamlessly with any...

Jake Minard | Oct 10, 2024

Content Search with Optimizely Graph

Optimizely Graph lets you fetch content and sync data from other Optimizely products. For content search, this lets you create custom search tools...

Dileep D | Oct 9, 2024 | Syndicated blog

Omnichannel Analytics Simplified – Optimizely Acquires Netspring

Recently, the news broke that Optimizely acquired Netspring, a warehouse-native analytics platform. I’ll admit, I hadn’t heard of Netspring before,...

Alex Harris - Perficient | Oct 9, 2024 | Syndicated blog

Problem with language file localization after upgrading to Optimizely CMS 12

Avoid common problems with xml file localization when upgrading from Optimizely CMS 11 to CMS 12.

Tomas Hensrud Gulla | Oct 9, 2024 | Syndicated blog

Optimizely Autocomplete (Statistics)

A user starts typing in the search input, and it returns suggestions for phrases they might be searching for. How to achieve this?

Damian Smutek | Oct 9, 2024 | Syndicated blog

Optimizely Forms: You cannot submit this form because an administrator has turned off data storage.

Do not let this error message scare you, the solution is quite simple!

Tomas Hensrud Gulla | Oct 4, 2024 | Syndicated blog