Using the "Take" LINQ method against a DynamicNodeList?
I guess I'm getting a little confused with the new Dynamic types and LINQ. So I'm guessing that we don't have full access to LINQ methods against the DynamicNodeList? For instance, the following gives me an error:
@foreach(dynamic page in Model.Children.Take(5)) { @* Do something *@ }
And I get the error:
Error loading Razor Script 'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'Take'
I know I could use old school ways to only pull the first 5, but is there any way to use LINQ to do this?
Ok, so for the time being I'm having to do something like this:
<umbraco:Macro runat="server" language="razor">
@using System.Linq
@using System.Xml.Linq
@{
var node = new DynamicNode(Model.Id);
var pages = node.GetChildrenAsList.Items.AsQueryable().OrderByDescending(tt => DateTime.Parse(tt.GetProperty("publishDate").Value));
var firstPage = pages.First();
<div id="first-page">
<h1>@firstPage.Name</h1>
<p><a href="@firstPage.Url">Listen</a></p>
</div>
<div id="remaining-pages">
foreach(dynamic page in pages.Skip(1).Take(5)) {
<div>
<h1>@page.Name</h1>
<p><a href="@page.Url">Listen</a></p>
</div>
}
</div>
}
</umbraco:Macro>
Setting the node and pages variables seems like a bit of overkill to get what I'm trying to access. Shouldn't just doing Model.Children.OrderBy("publishDate descending").First() work?
Error loading Razor Script
'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'First'
What's going on here? I've made sure I followed the upgrade guide, I even used the 4.7 web.config and just changed the umbracoDbDSN and umbracoConfigurationStatus nodes, and then added the uComponents references in it. I'm not sure what else I can do. This is very strange :-/
Very odd - i've checked those methods are definitely working.
I might need to get a copy of your umbraco site w/ database so I can run the debugger against it - obviously something is going wrong in the dynamic resolution of the extension methods. This is likely to be a file in the bin folder or similar but I won't know until I can see it in the debugger.
Is this something you could supply to me?
You can skype me with the username agrath - I'm on NZ time, so it's 10:40 am now
Spoke with Gareth on Skype and he figured out it was 2 dlls causing problems. I had to remove the Microsoft.Dynamic.dll and also Microsoft.Scripting.Debugging.dll. Posting this just in case someone else runs into a similar issue in the future.
NHibernate is another DLL that seems to cause this issue, which is a problem because a number of umbraco packages seem to use it. See the work item http://umbraco.codeplex.com/workitem/30570 on the Umbraco code plex page for instructions to reproduce the issue from a base install of Umbraco 4.7.1 and vote if this reflects the problems occurring in your applicaiton.
was working, but uninstalling Umbraco Contour removed my Umbraco.Forms.Core.dll, which contains the "Take" extension method (I guess, did not bother to have a look at the source code).
Hence, after recovering the Umbraco.Forms.Core.dll, my template just work...again.
In short, all assemblies need all their dependencies in 4.7.1.x to be present for the extension method discovery to work.
If you're having a problem, make sure you're running 4.7.1 and you'll see a lot more debugging in the ?umbDebugShowTrace=true output which should help to identify the offending DLL and then either remove it, or add whichever dependency is required (often when you've upgraded a site, or replaced a plugin, some DLLs are left behind but their dependencies are gone or replaced)
Hope this generalised answer identifies the issue for someone later.
Microsoft.Dynamic.dll and also Microsoft.Scripting.Debugging.dll - from where you removed it? You had it in project references?(I dont have these references and still have a problem).
Using the "Take" LINQ method against a DynamicNodeList?
I guess I'm getting a little confused with the new Dynamic types and LINQ. So I'm guessing that we don't have full access to LINQ methods against the DynamicNodeList? For instance, the following gives me an error:
And I get the error:
I know I could use old school ways to only pull the first 5, but is there any way to use LINQ to do this?
Thanks!
Are you referencing the LINQ assembly?
Yep, here's my macro code:
Actually, using "First()" doesn't work either.
Model.Children.OrderBy("publishDate descending").First()
Is this expected behavior, or am I doing something wrong, or is this a bug?
Ok, so for the time being I'm having to do something like this:
Setting the node and pages variables seems like a bit of overkill to get what I'm trying to access. Shouldn't just doing Model.Children.OrderBy("publishDate descending").First() work?
Hi Michael
Yes, the Linq extension methods should be working off DynamicNodeList
I've just checked with my copy, and they do - what version are you running?
If it's 4.7 final, that's very odd - I haven't changed anything to do wtih DyamicNodeList & Enumerable methods in quite a while.
I'm running 4.7 final. It was an upgrade from 4.6.2 so maybe I'm missing something?
I'm still having issues. The same thing on another site I'm building that was an upgrade as well. Here is a simple macro that doesn't work:
I get the following error:
What's going on here? I've made sure I followed the upgrade guide, I even used the 4.7 web.config and just changed the umbracoDbDSN and umbracoConfigurationStatus nodes, and then added the uComponents references in it. I'm not sure what else I can do. This is very strange :-/
Hi Michael
Very odd - i've checked those methods are definitely working.
I might need to get a copy of your umbraco site w/ database so I can run the debugger against it - obviously something is going wrong in the dynamic resolution of the extension methods. This is likely to be a file in the bin folder or similar but I won't know until I can see it in the debugger.
Is this something you could supply to me?
You can skype me with the username agrath - I'm on NZ time, so it's 10:40 am now
Gareth
Sure thing. I really appreciate it! Will call you in a minute. Thanks!
Spoke with Gareth on Skype and he figured out it was 2 dlls causing problems. I had to remove the Microsoft.Dynamic.dll and also Microsoft.Scripting.Debugging.dll. Posting this just in case someone else runs into a similar issue in the future.
Thanks so much Gareth! You're a badass!
I have this same problem but I don't have those DLLs. Any ideas?
Here is my code:
@using System.Linq
@using System.Xml.Linq
@inherits umbraco.MacroEngines.DynamicNodeContext
@helper getPageTitle(dynamic node)
{
<text>@(!String.IsNullOrEmpty(@node.Title) ? @node.Title : @node.Name)</text>
}
<ul>
@{
var news = @Model.NodeById(@Parameter.Group).Children;
news = news.Where("nodeTypeAlias.Equals(\"Special\")").OrderBy("publicationDate desc");
if (@Parameter.IsArchive == "1") {
news = news.Where("expirationDate < DateTime.Now");
} else {
news = news.Where("expirationDate > DateTime.Now && publicationDate < DateTime.Now");
}
var limit = 0;
int.TryParse(@Parameter.Limit, out limit);
if (limit > 0) {
news = news.Take(limit);
}
}
@foreach(var item in @news)
{
<li><a href="@item.Url">@getPageTitle(@item)</a>
<span>@item.publicationDate.ToString("MMM d, yyyy")</span></li>
}
</ul>
I couldn't get it working dynamicaly. So I had to cast everything as its type.
@using System.Linq
@using System.Xml.Linq
@using umbraco.MacroEngines
@inherits DynamicNodeContext
@helper getPageTitle(dynamic node)
{
<text>@(!String.IsNullOrEmpty(@node.Title) ? @node.Title : @node.Name)</text>
}
<ul>
@{
List<DynamicNode> news = ((DynamicNodeList)@Model.NodeById(@Parameter.Group).Children).Items;
//news = news.Where("nodeTypeAlias.Equals(\"Special\")").OrderBy("publicationDate desc");
news = news.Where(x => x.NodeTypeAlias.Equals("Special")).OrderByDescending(x => x.GetProperty("publicationDate").Value).ToList();
if (@Parameter.IsArchive == "1")
{
//news = news.Where("expirationDate < DateTime.Now");
news = news.Where(x => DateTime.Parse(x.GetProperty("expirationDate").Value) < DateTime.Now).ToList();
}
else
{
//news = news.Where("expirationDate > DateTime.Now && publicationDate < DateTime.Now");
news = news.Where(x => DateTime.Parse(x.GetProperty("expirationDate").Value) > DateTime.Now
&& DateTime.Parse(x.GetProperty("publicationDate").Value) < DateTime.Now).ToList();
}
int limit = 0;
int.TryParse(@Parameter.Limit, out limit);
if (limit > 0)
{
news = news.Take(limit).ToList();
}
}
@foreach (DynamicNode item in @news)
{
<li><a href="@item.Url">@getPageTitle(@item)</a>
<span>@String.Format(item.GetProperty("publicationDate").Value, "MMM d, yyyy")</span></li>
}
</ul>
NHibernate is another DLL that seems to cause this issue, which is a problem because a number of umbraco packages seem to use it. See the work item http://umbraco.codeplex.com/workitem/30570 on the Umbraco code plex page for instructions to reproduce the issue from a base install of Umbraco 4.7.1 and vote if this reflects the problems occurring in your applicaiton.
In my case,
was working, but uninstalling Umbraco Contour removed my Umbraco.Forms.Core.dll, which contains the "Take" extension method (I guess, did not bother to have a look at the source code).
Hence, after recovering the Umbraco.Forms.Core.dll, my template just work...again.
No
@using System.Linq
@using System.Xml.Linq
required.
In short, all assemblies need all their dependencies in 4.7.1.x to be present for the extension method discovery to work.
If you're having a problem, make sure you're running 4.7.1 and you'll see a lot more debugging in the ?umbDebugShowTrace=true output which should help to identify the offending DLL and then either remove it, or add whichever dependency is required (often when you've upgraded a site, or replaced a plugin, some DLLs are left behind but their dependencies are gone or replaced)
Hope this generalised answer identifies the issue for someone later.
Gareth
hey guys how do i check the bin folder for errors guys
I have the same error.
Microsoft.Dynamic.dll and also Microsoft.Scripting.Debugging.dll - from where you removed it? You had it in project references?(I dont have these references and still have a problem).
is working on a reply...