Using Sitefinity’s Dynamic LINQ to filter classifications

By February 17, 2014Sitefinity

What is Dynamic Linq anyway?

Dynamic LINQ has been around in the .NET space for a while. To know more about what Dynamic Linq is and how to use it in general (out of Sitefinity) please head to Scott Gu’s post. Now lets learn about Dynamic Linq in Sitefinity’s context.

Sitefinity has implemented its own version of Dyanmic Linq under Telerik.Sitefinity.Data.Linq.Dynamic namespace. In this post we will see how you, the reader, can you Sitefinity’s Dynamic Linq and filter List Items based on classification.

Why would I need Dynamic Linq?

Dynamic Linq as Scott Hanselman describes makes custom Linq expressions easier. We all know that Sitefinity has custom fields which are not (can’t be) exposed on the POCO model. For example lets lets say we have a custom classification on Sitefinity’s News Item which we added through custom fields in Sitefinity.

newsitem-customtopic

So this custom field does not exist on NewsItem.cs (we can use a decompiler to be sure) which makes writing type safe Linq queries not possible. This is where Dynamic Linq comes in which adds an aspect of Dynamic queries to regular Linq.

Show me the code!

Let’s get right to the code, see below for a method which will build the Dynamic Linq Query we are looking for

public string BuildQuery(string taxonomyName, string taxonName)
{
string query = string.Empty; 

const string dynamicContainsFormat = “{0}.Contains(({1}))”; 

var taxon = GetTaxonByName(taxonomyName, taxonName);
if (taxon != null)
{
query =
string.Format(dynamicContainsFormat, taxonomyName, taxon.Id);
}
 

return query;
}
 

public Taxon GetTaxonByName(string taxonomyName, string taxonName)
{
Taxon taxon = null;
var taxonomyManager = TaxonomyManager.GetManager(TaxonomyManager.GetDefaultProviderName());
var vals = taxonName.Split(new char[] { ‘/’ }, StringSplitOptions.RemoveEmptyEntries); 

Taxonomy taxonomy = taxonomyManager.GetTaxonomies<Taxonomy>().
SingleOrDefault(t => t.Name == taxonomyName);
 

if (taxonomy != null)
{
if (taxonomy is FlatTaxonomy)
{
var urlName = vals[0];
taxon = taxonomyManager.GetTaxa<
FlatTaxon>().
FirstOrDefault(t => (t.Taxonomy.Name == taxonomyName) && (t.UrlName == urlName));
}
else if (taxonomy is HierarchicalTaxonomy)
{
var urlName = vals[0];
taxon = taxonomyManager.GetTaxa<
HierarchicalTaxon>().
FirstOrDefault(t => (t.Taxonomy.Name == taxonomyName) && (t.UrlName == urlName));
if (taxon != null && vals.Length > 1)
{
for (int i = 1; i < vals.Length; i++)
{
var parentTaxonName = taxon.Name;
taxonName = vals[i];
taxon = taxonomyManager.GetTaxa<
HierarchicalTaxon>().
FirstOrDefault(t => (t.Parent.Name == parentTaxonName) && (t.UrlName == taxonName));
}
}
}
}
 

return taxon;
}

 

The Build query method takes taxonomy name, in our case “topics”, and taxon name; which is the taxon with which we are trying to filter the news items by. The GetTaxonByName method finds the taxon in the give taxonomy. Once we run this method we will get a dynamic query which is the following format “topics.contains(4e724ef2-5d1f-4179-8038-cd1a8a9aa6c3)” which when run on list of news items filters the news items to a given topic.

var manager = NewsManager.GetManager(); 

var newsItems = manager.GetNewsItems().Where(ni => ni.Status == ContentLifecycleStatus.Live && ni.Visible); 

//Builds the dynamic query
string query = BuildQuery(“topics”, “topic1”); 

//Using Dynamic LINQ to filter the items
var filteredNewsItems = newsItems.Where(query);

Please be sure to include Sitefinity’s Dynamic Linq namespace to have your solution compiled.

using Telerik.Sitefinity.Data.Linq.Dynamic;

That’s it. Its that easy to use Sitefinity’s Dynamic Linq library. Happy coding!

The following two tabs change content below.