Get Distinct Years from a DateTime field in a dynamic module

Posted by Community Admin on 04-Aug-2018 10:42

Get Distinct Years from a DateTime field in a dynamic module

All Replies

Posted by Community Admin on 10-Jul-2014 00:00

I have a dynamic module which have a datetime field [Let's Say Start Date]. I have number of items of that modules. What i want's is i can get Distinct Year for all the items. I want's to avoid enumerating all items and get distinct years one by one.Want's it to check if some thing can be done via API

How can i get it?

 

Posted by Community Admin on 10-Jul-2014 00:00

Hi,

You could get a generic list of all the items through the API. Then do something like this:

List<string> allYears = dynamicList.Select(x => x.StartDate.Value.Year.ToString()).ToList();

Best regards,
Daniel

Posted by Community Admin on 10-Jul-2014 00:00

Thanks Daniel. So I have to get full List in any case. 

Posted by Community Admin on 10-Jul-2014 00:00

Hi,

Well yes, in my example I used a generic list.
If you query the DynamicModuleManager it returns an IQueryable, so that is not too bad I think?

var dates = manager.GetDataItems(type).Select(x => x.DateCreated.Year.ToString(CultureInfo.InvariantCulture));

Best regards,
Daniel

Posted by Community Admin on 15-Aug-2017 00:00

Hi Daniel, 

In your example code how are you using x.StartDate directly in the Select expression because StartDate is in theory a dynamic field so the compiler shouldn't even be aware of it? 

I'm trying something similar. I have Investment Fund as a parent content type and Historic Data as a child content type the latter of which has a Date field. My requirement is to allow the user to download historic records between two sets of dates, but the code below returns zero even though there are definitely records between those dates: 

var childItems = fundItem.GetChildItems(typeHistoricData)
    .Where(x => x.Status == ContentLifecycleStatus.Live
    && x.Visible == true
    && x.GetValue<DateTime?>("Date") > newFromDate
    && x.GetValue<DateTime?>("Date") < newToDate).ToList();
 
var childItems2 = dynamicModuleManager.GetDataItems(typeHistoricData).Where(x => x.SystemParentId == fundItem.OriginalContentId
    && x.Status == ContentLifecycleStatus.Live
    && x.Visible == true
    && x.GetValue<DateTime?>("Date") > newFromDate
    && x.GetValue<DateTime?>("Date") < newToDate).ToList();

This thread is closed