LINQ for looping through pages and checking a property value
Hi All,
Can someone please point where i am going wrong with this linq?
var photoStaffList = siteRoot.Descendants("staffProfile").Where(x => x.GetPropertyValue<string>("role").Contains("photo")).ToList();
I want to find all the pages that are of type 'staffProfile' and then loop through each of those pages checking whether a value set in the role poperty (dataType multi dropdown picker) contains the word 'photo'.
var siteRoot = Model.Content.AncestorOrSelf(1);
var photoStaffList = (from c in siteRoot.Descendants()
where c.ContentType.Alias.Equals("staffProfile", StringComparison.CurrentCultureIgnoreCase)
&& c.GetPropertyValue<String>("role").Contains("photo", StringComparison.CurrentCultureIgnoreCase)
select c).ToList();
Going back to your original code - I think you've found a bug in how Umbraco converts the Multiple Dropdown to a string in Linq. I've just tried this and I get a similar issue.
The only workaround I can see is to get all the descendants and then test that property in a foreach loop.
My guess is that GetPropertyValue<string>("role") is returning a CSV of prevalues (e.g., "123,456,789") rather than a CSV of strings (e.g., "role1,role2,role3"). Here are some things you can do to investigate.
@{
var nodes = siteRoot.Descendants("staffProfile").ToArray();
var values = string.Join(" * ", nodes.Select(x => x.GetPropertyValue<string>("role")).ToArray());
}
<p>There are @(nodes.Length) nodes and the values are @(values).</p>
I'm not in an IDE right now, so I'm not sure if all that syntax is correct, but you get the idea.
Thanks for the responses with this one. @Nicholas i will give your solutions a go and see what i get :)
Using @Ben's posts, i can achieve what i want with the following code
//gets all the photography staff members
var photoStaffList = (from c in siteRoot.Descendants()
where c.ContentType.Alias.Equals("staffProfile", StringComparison.CurrentCultureIgnoreCase)
&& c.GetPropertyValue<string>("role").ToLower().Contains("photo")
select c).ToList();
LINQ for looping through pages and checking a property value
Hi All,
Can someone please point where i am going wrong with this linq?
I want to find all the pages that are of type 'staffProfile' and then loop through each of those pages checking whether a value set in the role poperty (dataType multi dropdown picker) contains the word 'photo'.
I keep getting 0 results returned.
Thanks Paul
Try this....
Hi Ben,
Thans for the response, im getting the following compile error using your code
Thanks Paul
Going back to your original code - I think you've found a bug in how Umbraco converts the Multiple Dropdown to a string in Linq. I've just tried this and I get a similar issue.
The only workaround I can see is to get all the descendants and then test that property in a foreach loop.
You should report this as a bug I think.
My guess is that
GetPropertyValue<string>("role")
is returning a CSV of prevalues (e.g., "123,456,789") rather than a CSV of strings (e.g., "role1,role2,role3"). Here are some things you can do to investigate.I'm not in an IDE right now, so I'm not sure if all that syntax is correct, but you get the idea.
Hi All,
Thanks for the responses with this one. @Nicholas i will give your solutions a go and see what i get :)
Using @Ben's posts, i can achieve what i want with the following code
Be good to have a play around though.
Many thanks Paul
is working on a reply...