Get Distinct Years from a DateTime field in a dynamic module - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Get Distinct Years from a DateTime field in a dynamic module

  • Get Distinct Years from a DateTime field in a dynamic module
  • 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?

     

  • 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

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

  • 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

  • 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();