Filter pages using LINQ queries

Sitefinity CMS supports dynamic LINQ queries that can execute lambda expressions defined in a string against an IQueryable collection. This article demonstrated how to filter monolingual and multilingual pages using two different methods: pages.Where() and DataProviderBase.SetExpressions().

Use the pages.Where() method 

To filter a collection of pages by their title, perform the following:

  1. Use the PageManager class to get all pages under the root node of your site.
  2. Construct the string used to filter those pages by their title.
  3. Filter the page collection by passing the constructed string filter to the Where() method.

To filter pages with the pages.Where() method, use the following code snippet:

NOTE: To execute dynamic LINQ queries, in your code, you must add using Telerik.Sitefinity.Data.Linq.Dynamic;

Filter multilingual pages with the pages.Where() method

If you have multilingual pages, to ensure the filter you pass is executed against the Lstring value for the correct culture you can use the following code sample:

  1. Use the PageManager class to get all pages under the root node of your site.
  2. Set the desired thread culture.
  3. Construct the string used to filter those pages by their title.
  4. Filter the page collection by passing the constructed string filter to the Where() method.
  5. Set the original thread culture.

To filter multilingual pages with the pages.Where() method, use the following code snippet:

Use the DataProviderBase.SetExpressions() method

  1. Use the PageManager class to get all pages under the root node of your site.
  2. Construct the string used to filter those pages by their title.
  3. Filter the page collection by calling the DataProviderBase.SetExpressions() method and passing the following parameters:
    • filterExpression
    • sortExpression
    • skip
    • take
    • totalCount

To filter pages with the DataProviderBase.SetExpressions() method, use the following code snippet:

NOTE: To use the DataProviderBase class, in your code, you must add using Telerik.Sitefinity.Data;

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Web Security for Sitefinity Administrators

The free standalone Web Security lesson teaches administrators how to protect your websites and Sitefinity instance from external threats. Learn to configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?

Next article

Pages events