Create a list of nodes based on a datepart of CreateDate
Hi Guys, doe's any one know how to achieve the following.
Using another post I have managed to create an archive of unique months and years from a list of articles.
However I don't know how to write the razor query to list the headlines based on what month/year I am at.
In codeified english it would look like this.
foreach( var article in nodes.Where( THE YEAR PART OF CreateDate IS EQUAL TO yearVariable AND MONTH PART OF CreateDate IS EQUAL TO THE monthVariable) )
{
Headline
}
Could anyone help with with the code needed to list the articles based on a given year month of createdate.
I have used similar before getting a course name from a list of courses. In this case I tried your code with the variables in and got the following error.
"Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"
I am looking up what this means but it's still open.
Ok for anyone interested I have cleaned it up a bit as the second set of variables were for me to check where the hell it broke :)
What this does is create a list of article headlines by YEAR - MONTH without having to use date folders. The styles are related to an accordion but could be just UL LI etc. and the node number is the parent node of a bunch of articles with the page type Article.
Thanks again.
Rob
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
var blogNode = Umbraco.TypedContent(1147);
var articles = blogNode.Descendants("Article").OrderBy("CreateDate");
int lastYear = 0;
int lastMonth = 0;
}
<div class="archive">
<h2>Archive</h2>
<ul class="archivenav">
@foreach (var e in articles)
{
int year = e.CreateDate.Year;
int month = e.CreateDate.Month;
if (year > lastYear)
{
<li class="toggle-trigger">
@year
</li>
}
if (year != lastYear || month > lastMonth)
{
<div class="toggle-box" style="">
<li class="toggle-trigger">
@e.CreateDate.ToString("MMM")
</li>
<div class="toggle-box" style="">
@foreach( var node in articles.Where(x => x.CreateDate.Year == year && x.CreateDate.Month == month))
{
var title = node.GetPropertyValue<string>("copyHeader");
<li>@title</li>
}
</div>
</div>
}
lastYear = year;
lastMonth = month;
}
</ul>
</div>
Create a list of nodes based on a datepart of CreateDate
Hi Guys, doe's any one know how to achieve the following.
Using another post I have managed to create an archive of unique months and years from a list of articles.
However I don't know how to write the razor query to list the headlines based on what month/year I am at.
In codeified english it would look like this.
foreach( var article in nodes.Where( THE YEAR PART OF CreateDate IS EQUAL TO yearVariable AND MONTH PART OF CreateDate IS EQUAL TO THE monthVariable) ) {
Headline
}
Could anyone help with with the code needed to list the articles based on a given year month of createdate.
Kind Regards
Doogie
Hi Doogie,
Did you try like that:
Best
I have used similar before getting a course name from a list of courses. In this case I tried your code with the variables in and got the following error.
"Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"
I am looking up what this means but it's still open.
Thanks for your help.
Hi Doogie,
You can use strongly typed nodes. Can you show your current code ?
Thanks
The only change you need to do is
var blogNode = Umbraco.Content(1147);
change to:
var blogNode = Umbraco.TypedContent(1147);
After that everything is working fine. I don't like dynamic type at all.
Thanks,
Alex
Cool after your wee change above I changed the test bit to.
And everything works fine.
Thanks very much Alex.
Doogie, you are welcome !
Ok for anyone interested I have cleaned it up a bit as the second set of variables were for me to check where the hell it broke :)
What this does is create a list of article headlines by YEAR - MONTH without having to use date folders. The styles are related to an accordion but could be just UL LI etc. and the node number is the parent node of a bunch of articles with the page type Article.
Thanks again.
Rob
is working on a reply...