.Where statement to pull in records where an Content ID is apart of a multi-node treepicker list
Hi all,
I'm trying to pull in a list of case studies where a content ID (this is a user page, so sort of a user ID) existings within a multi-node treepicker field (each case study page has a list of users involved in each project).
So....
Case Study Node: projectMembers field (multi-node treepicker field which contains IDs of content pages from teams page).
FYI: the "1107" is just hardcoded it's an ID that was listed on the page. The ID. I'd also need to support multiple IDs, so contains a bunch of ID's...
It's basically a multi search features, I have a list of users & a list of categories. if a person has selected two users, it will search for products with both of their IDs involved :).
foreach(var item in Model.Content.Site().FirstChild("caseStudies").Descendants("caseStudy").Where(x => x.GetPropertyValue("projectMembers").Contains("1107")))
{
//echo out ID in here, I can do this part :)
}
This is code I've found being used around the place but doesn't seem to work for my case...
I'm getting strange results here, using your code & the ID of 1107 (which I can confirm is the node ID of a team page user).
This returns 0 results.
However when I change the ID in the Contains statement to just Contains("1") it returns every case study (even ones with no value in the multi node tree picker.
Can you think of any reason this would be the case?
FYI: I'm using Umbraco 7.6.7 if that makes a difference -> I know the IDs have changed between 7.5 -> 7.6 so could be something there. I thought it may be searching for guids rather than node IDs but doesn't seem to be the case.
var selected_members = new List<int>();
if(!Request.Params["member"].IsEmpty())
{
foreach(var member in Request.Params["member"].Split(','))
{
selected_members.Add(Convert.ToInt32(member));
}
}
@foreach(var mem in selected_members)
{
<p>Mem: @mem</p>
}
// This returns...
Mem: 1107
Mem: 1108
So is there a function like ArrayContains(selected_members)?
foreach(var item in Model.Content.Site().FirstChild("caseStudies").Descendants("caseStudy").Where(x => x.GetPropertyValue<IEnumerable<IPublishedContent>>("projectMembers").Select(y => y.Id).Array.IndexOf(selected_members)).Select(x => x.Id))
I have found IndexOf, but unsure of how to go about using it :).
Appreciate it, others will probably be curious on how to do this as well.
Not sure what method I'm going to use yet, but would it be tricky to find ALL projects that have at least one of the "selected_members" within?
So selected members are... 1107, 1108 & 1112
Projects as followed...
Project #1: contains 1107, 1113
Project #2: contains 1108, 1112
Project #3: contains 1106, 1107
So, for our query above of searching for projects with 1107, 1108 & 1112 it would return all projects because atleast one of the members is in each
At the moment it requires all of the selected members to be within the project for it to be returned as a result.
Also, do you have any recommendations for guides on learning how to best make use of .where() / Linq, I'm from a PHP background so more use to SQL but I'm slowly understanding it!
I have solved this one myself, it was actually a simple change. Just had to check that the returns members was equal or more than 1, rather than seeing if it matched the amount of selected users.
.Where statement to pull in records where an Content ID is apart of a multi-node treepicker list
Hi all,
I'm trying to pull in a list of case studies where a content ID (this is a user page, so sort of a user ID) existings within a multi-node treepicker field (each case study page has a list of users involved in each project).
So....
Case Study Node: projectMembers field (multi-node treepicker field which contains IDs of content pages from teams page).
FYI: the "1107" is just hardcoded it's an ID that was listed on the page. The ID. I'd also need to support multiple IDs, so contains a bunch of ID's...
It's basically a multi search features, I have a list of users & a list of categories. if a person has selected two users, it will search for products with both of their IDs involved :).
This is code I've found being used around the place but doesn't seem to work for my case...
CS1593: Delegate 'System.Func
Hi Hayden
Try this code:
Thanks,
Alex
Hi Alex,
I'm getting strange results here, using your code & the ID of 1107 (which I can confirm is the node ID of a team page user).
This returns 0 results.
However when I change the ID in the Contains statement to just Contains("1") it returns every case study (even ones with no value in the multi node tree picker.
Can you think of any reason this would be the case?
FYI: I'm using Umbraco 7.6.7 if that makes a difference -> I know the IDs have changed between 7.5 -> 7.6 so could be something there. I thought it may be searching for guids rather than node IDs but doesn't seem to be the case.
Cheers, Hayden
Hayden,
Is "Property Values Converter" enabled in your project?
If yes, the code should be:
Hi Alex,
I can confirm that EnablePropertyValueConverters is set to true
When I run the code above I get the error I had originally...
I'm assume the > after GetPropertyValue was a mistake :)?
CS1593: Delegate 'System.Func
Hi Alex
Ignore my above post, I have miscopied your snippet :).
It is now working!
Thank you so much, have been banging my head on this for awhile!!
Cheers, Hayden
Hayden, really glad to help you! Ask questions if you have and have a great evening!
Hi Alex,
I was wondering if there's an easy way to convert this to accept multiple IDs.
So say a user has selected users 1107, 1108 and want to find any case studies that these two IDs are involved in.
Is there a function similar to contain that accepts an array maybe?
This is what I have at the moment...
URL: /case-studies/?search=1&member=1107&member=1108
So is there a function like ArrayContains(selected_members)?
I have found IndexOf, but unsure of how to go about using it :).
Appreciate it, others will probably be curious on how to do this as well.
Hi Hayden
It looks like you need this code:
Legend! no wonder you have an MVP tag :).
That works perfectly, hopefully will help other community members as well.
Cheers, Hayden
Thanks, Hayden for your words )
Hi Alex,
Not sure what method I'm going to use yet, but would it be tricky to find ALL projects that have at least one of the "selected_members" within?
At the moment it requires all of the selected members to be within the project for it to be returned as a result.
Also, do you have any recommendations for guides on learning how to best make use of .where() / Linq, I'm from a PHP background so more use to SQL but I'm slowly understanding it!
Cheers, Hayden
Hi Alex,
I have solved this one myself, it was actually a simple change. Just had to check that the returns members was equal or more than 1, rather than seeing if it matched the amount of selected users.
For anybody who's interested in this method :)
Hi Hayden
Thanks for sharing with the community!
Alex
is working on a reply...