I have a requirement where I have to search articles under a specific section within Umbraco and return the best possible match (from keywords entered). The code block I am using is below:
The part where I am using (x => Convert.ToString(x.GetPropertyValue("metaKeywords")).ToLower().Contains(SearchTerm)) within FirstOrDefault() the code fails. If I remove it the page works fine. But this is a completely valid line of code. Whay it would fail on this line?
if (SearchType == "licence")
{
var articles = Umbraco.Content(1080).Descendants();
// This line works perfectly fine, I can get result and redirect to the page
var result = articles.Where("Visible && DocumentTypeAlias == \"GenericContentArticle\" && Id = " + SearchId + "").FirstOrDefault();
if (result != null)
{
Response.Redirect(result.Url);
}
}
}
else
{
SearchTerm = SearchTerm.ToLower();
// Here it fails, if I remove [x => Convert.ToString(x.GetPropertyValue("metaKeywords")).ToLower().Contains(SearchTerm)] part from the line then the error is resolved.
var result1 = articles.Where("Visible && DocumentTypeAlias == \"GenericContentArticle\"").FirstOrDefault(x => Convert.ToString(x.GetPropertyValue("metaKeywords")).ToLower().Contains(SearchTerm));
if (result1 != null)
{
Response.Redirect(result1.Url);
}
}
}
}
I will really appreciate, if you suggest a solution or direct me to one.
Failing to find content within Umbraco nodes
Hi,
I have a requirement where I have to search articles under a specific section within Umbraco and return the best possible match (from keywords entered). The code block I am using is below:
The part where I am using (x => Convert.ToString(x.GetPropertyValue("metaKeywords")).ToLower().Contains(SearchTerm)) within FirstOrDefault() the code fails. If I remove it the page works fine. But this is a completely valid line of code. Whay it would fail on this line?
if (SearchType == "licence")
{
var articles = Umbraco.Content(1080).Descendants();
if (!string.IsNullOrEmpty(SearchTerm))
{
if (Request["licenceSearchKeywordsId"] != null)
{
string SearchId = Convert.ToString(Request["licenceSearchKeywordsId"]);
if (SearchId.Length > 0 && SearchId != "0")
{
// This line works perfectly fine, I can get result and redirect to the page
var result = articles.Where("Visible && DocumentTypeAlias == \"GenericContentArticle\" && Id = " + SearchId + "").FirstOrDefault();
if (result != null)
{
Response.Redirect(result.Url);
}
}
}
else
{
SearchTerm = SearchTerm.ToLower();
// Here it fails, if I remove [x => Convert.ToString(x.GetPropertyValue("metaKeywords")).ToLower().Contains(SearchTerm)] part from the line then the error is resolved.
var result1 = articles.Where("Visible && DocumentTypeAlias == \"GenericContentArticle\"").FirstOrDefault(x => Convert.ToString(x.GetPropertyValue("metaKeywords")).ToLower().Contains(SearchTerm));
if (result1 != null)
{
Response.Redirect(result1.Url);
}
}
}
}
I will really appreciate, if you suggest a solution or direct me to one.
Thanks,
Sudipta
is working on a reply...