2

I am in the process of updating Umbraco version. There is a blog, but the macro outputting blog posts is hiccuping. Originally, it was something like:

dynamic result = Model.Descendants().Where("NodeTypeAlias == \"BlogPost\"");
var values = new Dictionary<string,object>();
values.Add("currentDate", DateTime.Now) ;
result = result.Where("blogDate <= currentDate", values);

In v7.3, the debug error says

Error loading partial view macro (View: ~/Views/MacroPartials/Blog Post List.cshtml) 'object' does not contain a definition for 'Where'

I tried changing the Where cause to a different Lambda expression

Where(x => x.GetPropertyValue<DateTime>("blogDate") < values)

but then the error changes to

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

Any advice would be greatly appreciated. Thanks in advance!

Phillip Ng
  • 21
  • 2

3 Answers3

0

Are you sure there's data in Model? Maybe it's Model.Content? I also usually use DescendantsOrSelf(), maybe Descendants() isn't returning anything. The generic error message tells us nothing, so.maybe try breaking down your code a little, just to isolate the problem. What versions are you upgrading from?

Jannik Anker
  • 3,320
  • 1
  • 13
  • 21
0

Try by using the typed version (so you also have intellisense):

DateTime now = DateTime.Now;
var posts = Model.Content.Descendants("BlogPost").Where(n => n.GetPropertyValue<DateTime>("blogDate") < now);

foreach (IPublishedContent post in posts)
{
    ...
}
Eyescream
  • 902
  • 5
  • 14
0

Have you tried going with UmbracoHelper?

If your page inherits Umbraco.Web.Mvc.UmbracoViewPage then you will be able to structure your query like this:

var result = Umbraco.AssignedContentItem.Descendants().Where(x => x.DocumentTypeAlias == "BlogPost");

Otherwise, instantiate the UmbracoHelper first and then use it to access node information:

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
var result = umbracoHelper.AssignedContentItem.Descendants.Where(x => x.DocumentTypeAlias == "BlogPost");

If you don't have the assigned content (probably accessing data through AJAX) or current context, try hiding the node ID in HTML and transferring this info to your code.

Marko Jovanov
  • 418
  • 10
  • 25