Quantcast
Channel: Optimizely Search & Navigation
Viewing all articles
Browse latest Browse all 6894

Can I pass a reflected property into a TermsFacetFor method?

$
0
0

I am working on a search feature that adds different search facets to the search page depending on where the user is searching in a product catalog.  For example, if the user is searching over all products, the user might want to filter by manufacturer.  But if the user is searching the products of just a single manufacturer, they don't need a manufacturer filter and might want to filter by brand instead.

To try to reuse code and not write dozens of functions for all possible filters, I have created a generic function to get facets for each filter:

		public Dictionary<string, int> GetFilterCounts(IContentResult<SiteProductVariation> results, Expression<Func<SiteProductVariation, object>> fieldSelector)
		{
			Dictionary<string, int> facetResults = new Dictionary<string, int>();
			var facetItems = results.TermsFacetFor(fieldSelector);
			foreach (var item in facetItems)
			{
				facetResults.Add(item.Term.Replace('-', ''), item.Count);
			}
			return facetResults;
		}

However, the facets for each starting catalog node are stored as a list of strings on the node content.  To get from the string property name to the fieldSelector that the TermsFacetFor function needs I am trying to use System.Reflection but I cannot find a way to make it get the property in a way that TermsFacetFor can use.  Here is a list of what I've tried:

			var facetResult = search.GetFilterCounts(dataList, s => s.GetType().GetProperty(facet.Name).GetGetMethod());
			var facetResult = search.GetFilterCounts(dataList, s => s.GetType().GetProperty(facet.Name));

Both of these have resulted in null being returned from the TermsFacetFor call.  Is what I'm trying even possible?  If so, how can I manage it?


Viewing all articles
Browse latest Browse all 6894

Trending Articles