Thomas Krantz
Sep 8, 2009
  3364
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024