I am trying to filter content using Linq, i'm trying to return same doctypes as the current node and with the same value of a checklist property field value. I have tried the following
@{
var relatedDoc = Umbraco.ContentAtXPath("//relatedDocs");
foreach (var doc in relatedDoc .Where(x => x.Value("topic").ToString() == (Model.Topic).ToString()))
{
<p> @doc.Name</p>
}
}
and
@{
var relatedDoc = Umbraco.ContentAtXPath("//relatedDocs");
foreach (var doc in relatedDoc .Where(x => x.Value("topic").Equals(Model.Topic)))
{
<p> @doc.Name</p>
}
}
The above returns only the current node, I would like to return only the same doctypes with the same property value as the node current node not the current node
Does that mean a page can have multiple topics ticked??
If so then the stored value for a Topic won't be a 'single' topic, it's more likely to be a comma-separated list of topics? (depends on what the property editor is for the Checkbox list)
so if you have a topic with three checkboxes ticked
fishing, boating, fighting
then with your implementation above, that will only match articles with those three checkboxes ticked in that order.
eg another topic with only 'fishing' topic checked will not match as
"fishing" != "fishing,boating,fighting"
If the editor stores a comma delimited string like this then you would need to turn the string into an array, using String.Split() and then compare the two arrays using 'Intersect'
var currentArticleTopics = String.Split(Model.Value<string>("topic"), new char[]{','},StringSplitOptions.RemoveEmptyEntries);
foreach (var doc in relatedDoc .Where(x => String.Split(x.Value<string>("topic"), new char[]{','},StringSplitOptions.RemoveEmptyEntries).Intersect(currentArticleTopics).Count() > 0));
Typing on a phone, so fingers crossed that example gives you the gist if what you need, if not necessarily the most perfect way. Depending on the amount of content on your site, then you might find better performance using an Examine search query to look for the related articles.
Linq filtering
Hi Umbraco Team
I am trying to filter content using Linq, i'm trying to return same doctypes as the current node and with the same value of a checklist property field value. I have tried the following
and
The above returns only the current node, I would like to return only the same doctypes with the same property value as the node current node not the current node
Thanks Dibs
Hi Dibs
You say your Topic is a checkbox list?
Does that mean a page can have multiple topics ticked??
If so then the stored value for a Topic won't be a 'single' topic, it's more likely to be a comma-separated list of topics? (depends on what the property editor is for the Checkbox list)
so if you have a topic with three checkboxes ticked
fishing, boating, fighting
then with your implementation above, that will only match articles with those three checkboxes ticked in that order.
eg another topic with only 'fishing' topic checked will not match as
"fishing" != "fishing,boating,fighting"
If the editor stores a comma delimited string like this then you would need to turn the string into an array, using String.Split() and then compare the two arrays using 'Intersect'
Typing on a phone, so fingers crossed that example gives you the gist if what you need, if not necessarily the most perfect way. Depending on the amount of content on your site, then you might find better performance using an Examine search query to look for the related articles.
regards
Marc
Cheers Mark
for feedback
I was able to check my checklist to get related nodes/documents by the following
above returned all, except current, nodes of same doctype that had same checklist items selected
Dibs : )
is working on a reply...