Pretty new to Umbraco and found this forum super helpful so far but this is the first time I've not fould a solution in a previously asked question.
What I have is a list of companies that have a category propery each can have mulitple categories. I'm trying to get a list of similar companies so if Bloggs & Co are in Cat1, Cat3 and Cat5 I would like a list of any other companies that are also in any of those categories.
Now the code below does give me that list however returns duplicates if the company is found in more than one of the categories which is often the case.
Am I missing an obvious way to return a distinct list or have I gone about it in totally the wrong way?
with a little switch to typed models from dynamics (mainly using Model.Content instead of CurrentPage) you can do it in a couple of lines (i think - haven't tested this exact code!)
var categoryFilter = "Category1, Category2, Category7";
// split the filters into a list.
var filter = categoryFilter.ToDelimitedList();
var selection = Model.Content.Site().FirstChild("exhibitorList").Children("exhibitor").Where(x => x.IsVisible());
// above can be quite slow consider :
// var selection = Umbraco.TypedContentAtXPath("//exhibitor");
// to get all exhibitors across the site - much quicker.
// check each item, to see if the filter contains any of the cateogries in it (split into a list)
var exhibitors =
selection
.Where(x => filter.ContainsAny(x.GetPropertyValue<string>("categories", "").ToDelimitedList(",")))
.Distinct();
probably to note that is a case sensitive search , so you would need to make sure everything was the same.
you could do it without splitting the first list :
var exhibitors =
selection
.Where(x => categoryFilter.ContainsAny(x.GetPropertyValue<string>("categories", "").ToDelimitedList(","), StringComparison.InvariantCultureIgnoreCase))
.Distinct();
Finding Related Items
Hi Everyone,
Pretty new to Umbraco and found this forum super helpful so far but this is the first time I've not fould a solution in a previously asked question.
What I have is a list of companies that have a category propery each can have mulitple categories. I'm trying to get a list of similar companies so if Bloggs & Co are in Cat1, Cat3 and Cat5 I would like a list of any other companies that are also in any of those categories.
Now the code below does give me that list however returns duplicates if the company is found in more than one of the categories which is often the case.
Am I missing an obvious way to return a distinct list or have I gone about it in totally the wrong way?
Any help at all would be much appreciated!
Thanks :)
Hi
with a little switch to typed models from dynamics (mainly using Model.Content instead of CurrentPage) you can do it in a couple of lines (i think - haven't tested this exact code!)
probably to note that is a case sensitive search , so you would need to make sure everything was the same.
you could do it without splitting the first list :
its just a bit longer for the answer?
Hi Kevin
Thanks so much for taking the time to answer, that worked perfectly for me.
Dread to think how long I'd spent trying to figure this out!
Thanks again Cameron
is working on a reply...