I have a very simple issue (it seems) - Id like to get distinct parent nodes from a DynamicNodeList.
I have no trouble creating a new DynamicNodeList with the parent nodes, but want to get rid of the duplicates. Distinct on Items throws no errors, but I suspect that dynamic nodes are all distinct.
So the solution seems to be only to add items that doesn't already exists in the DynamicNodeList:
@{ DynamicNodeList uniqueList = new DynamicNodeList(); } <ul> @foreach (DynamicNode item in followedNodes) { if (uniqueList.Items.Where("Id == item.Id").Count() < 1) { uniqueList.Add(item.Parent); } } @foreach (DynamicNode item in uniqueList.Items.Distinct()) { <li> <a href="@item.Url">@item.Name</a> </li> } </ul>
The above seems clumsy and throws an error with Where:'System.Collections.Generic.List' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' has some invalid arguments
I guess it's still far from ideal but a bit shorter solution:
<ul> @foreach(var g in followedNodes.GroupBy("Parent.Id")) { var p = ((IEnumerable<DynamicNode>)g).First().Parent; <li><a href="@p.Url">@p.Name</a></li> } </ul>
Groupby and taking the first of each seems clever. It does however return this error:
The type arguments for method 'umbraco.MacroEngines.DynamicNodeList.GroupBy(string)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Is it possible to use GroupBy on a DynamicNodeList or what might be wrong?
Just an idea: do you think it would be possible just to get the Ancestors() of the DynamicNodeList? It did throw an error, but seems like a very simple solution to the problem.
Well, I got it. You're trying to call the GroupBy method on the object casted to "DynamicNodeList". To work as expected it should be called strictly on a dynamic object. Cast your "followedNodes" to "dynamic" and everything will be all right.
PS: I often get confused about dynamics as well...
Something went wrong here - I don't know what but didn't get i working - Instead i created an oldschool list of Node Id's and used Distinct(), which worked out well. Have to look closer into the issue at a later stage.
check if Node is included in DynamicNodeList
I have a very simple issue (it seems) - Id like to get distinct parent nodes from a DynamicNodeList.
I have no trouble creating a new DynamicNodeList with the parent nodes, but want to get rid of the duplicates. Distinct on Items throws no errors, but I suspect that dynamic nodes are all distinct.
So the solution seems to be only to add items that doesn't already exists in the DynamicNodeList:
The above seems clumsy and throws an error with Where:'System.Collections.Generic.List' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' has some invalid arguments
Any ideas?
I guess it's still far from ideal but a bit shorter solution:
Groupby and taking the first of each seems clever. It does however return this error:
The type arguments for method 'umbraco.MacroEngines.DynamicNodeList.GroupBy(string)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Is it possible to use GroupBy on a DynamicNodeList or what might be wrong?
Just an idea: do you think it would be possible just to get the Ancestors() of the DynamicNodeList? It did throw an error, but seems like a very simple solution to the problem.
That's strange - I tried that code and it worked. Ok, perhaps I'll proceed with some investigation a bit later today :)
Well, I got it. You're trying to call the GroupBy method on the object casted to "DynamicNodeList". To work as expected it should be called strictly on a dynamic object. Cast your "followedNodes" to "dynamic" and everything will be all right.
PS: I often get confused about dynamics as well...
Something went wrong here - I don't know what but didn't get i working - Instead i created an oldschool list of Node Id's and used Distinct(), which worked out well. Have to look closer into the issue at a later stage.
Thanks for your effort Rodion :o)
is working on a reply...