I have this code, which is working great at picking two random nodes (Persons). But I would love if I could hide/filter "Person 1" if I am visiting that page or children of that page.
Page structure:
Home -- Person 1 -- -- History 1 -- -- History 2 -- Person 2 -- -- History 3 -- Person 3 -- -- History 4
@using umbraco.MacroEngines;
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var r = new Random();
var numberOfItems = 2;
List<DynamicNode> pages = new DynamicNode(Model.AncestorOrSelf("Home"))
.Descendants("Person")
.Items.Where(x => x.Visible)
.OrderBy(x => r.Next())
.Take(numberOfItems)
.ToList();
}
<div class="row">
@foreach (DynamicNode page in pages)
{
<div class="six columns">
<img src="http://placehold.it/350x150" />
<h2>@page.Name</h2>
<p>@page.GetProperty("introText")</p>
<p><a href="@page.Url">Read more...</a></p>
<p><strong>@page.GetProperty("personsName")</strong></p>
</div>
}
</div>
If I understand you correctly you just want to filter out the Person one in your DynamicNode list. You can do that in various ways depending on what suits you best.
But I want to filter out 'Person1' if I am visiting that node or child nodes of 'Person1'. Similar if I am visiting 'Person2' or on of Person2 child nodes. It has to show two other persons :)
I try to get the Model (The node you are on). Test if any of the ancestors or the node itself has an Id that matches the id of the node you try to iterate. And only take those that doesn't match.
I have added a ')' at the end of line 2, but I get this error:
CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
Random node by nodeTypeAlias - hide current page
Hi fine folks,
I have this code, which is working great at picking two random nodes (Persons). But I would love if I could hide/filter "Person 1" if I am visiting that page or children of that page.
Page structure:
Home
-- Person 1
-- -- History 1
-- -- History 2
-- Person 2
-- -- History 3
-- Person 3
-- -- History 4
Best regards!
Kim
Hi Kim
If I understand you correctly you just want to filter out the Person one in your DynamicNode list. You can do that in various ways depending on what suits you best.
After .Descendants("Person") you can skip one with .Skip(1) that should remove the first one.
Or sort it out with a .Where(x => x.Name == "Person1").
Some of it might not be entirely correct as I didn't open VS to check, but should give you some pointers :)
Hi Niels,
Thank you very much for your answer!
But I want to filter out 'Person1' if I am visiting that node or child nodes of 'Person1'. Similar if I am visiting 'Person2' or on of Person2 child nodes. It has to show two other persons :)
Best regards
Kim
Hi Kim
That makes a lot of sense. Well then you want to test if the DynamicNode is either the the same as the node your on or if it fits the nodes ancestors.
Some freehand coding that can get you started:
I try to get the Model (The node you are on). Test if any of the ancestors or the node itself has an Id that matches the id of the node you try to iterate. And only take those that doesn't match.
Hi Niels,
I think it's really close ...
I have added a ')' at the end of line 2, but I get this error:
CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
Hope you can help me once more :)
Best regards
I can't wait till I get better. Being sick really messes up my brain.
I think this works. :)
Hi again,
I sincerely thank you for your time!
It still comes with the same error :(
I hate working with dynamic. I love my intellisense way too much.
I can't even remember how it is with the versions anymore. Do you have uQuery? You might need some usings for uQuery, not sure.
This might work:
And it works with uQuery!
Thank you - I owe you a giant beer!
I'll take a beer any day ;) Glad I could help.
is working on a reply...