0

In Umbraco CMS I have created a 6 events. In that 3 were created in January and the other 3 were created in February.

I want to fetch the events created in current month. For that I have used query builder in Umbraco and the query was:

@{
    var selection = Umbraco.Content(Guid.Parse("9359beaa-b580-4f2c-8c8f-239f29e560b4"))
    .ChildrenOfType("aseanEventsItem")
    .Where(x => (x.CreateDate >= DateTime.Now.Date))
    .Where(x => x.IsVisible());
}
<ul>
    @foreach (var item in selection)
    {
        <li>
            <a href="@item.Url">@item.Name</a>
        </li>
    }
</ul>

It's working fine now. How I can get the current month?

Thom A
  • 88,727
  • 11
  • 45
  • 75
  • 2
    `.Where(x => x.CreateDate.Month == DateTime.Now.Month && x.CreateDate.Year == DateTime.Now.Year)` – NetMage Feb 25 '22 at 16:31
  • To enhance NetMage's suggestion, I recommend capturing `DateTime.Now` into a separate variable for use before making the query. It's possible, but not impossible, for there to be an end-of-year rollover during the where clause processing of `DateTime.Now.Month` and `DateTime.Now.Year` that would cause unintended results. – AlwaysLearning Feb 25 '22 at 22:42

1 Answers1

0

You can use the Model.Content.Children to do that. Here is an example and the screenshot for you:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using Umbraco.Core.Models
@using Umbraco.Web

@{
    Layout = null;

    var lastMonth = DateTime.Now.AddMonths(-1);

    var testRoot = Model.Content.Children
        .Where(x => x.CreateDate >= lastMonth);

    //var selection = Umbraco.Content(Guid.Parse("9359beaa-b580-4f2c-8c8f-239f29e560b4"))
    //    .ChildrenOfType("aseanEventsItem")
    //    .Where(x => (x.CreateDate >= DateTime.Now.Date))
    //    .Where(x => x.IsVisible());
}

<p>TEST template</p>

@foreach (var item in testRoot)
{
    <p>@item.Name - @item.CreateDate</p>
}

enter image description here

Nurhak Kaya
  • 1,470
  • 1
  • 18
  • 20