1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Get Distinct Years from a DateTime field in a dynamic module

Get Distinct Years from a DateTime field in a dynamic module

5 posts, 1 answered
  1. kamii47
    kamii47 avatar
    71 posts
    Registered:
    02 Mar 2007
    10 Jul 2014
    Link to this post

    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?

     

  2. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    10 Jul 2014 in reply to kamii47
    Link to this post

    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

  3. kamii47
    kamii47 avatar
    71 posts
    Registered:
    02 Mar 2007
    10 Jul 2014 in reply to Daniel Plomp
    Link to this post

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

  4. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    10 Jul 2014 in reply to kamii47
    Link to this post

    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

    Answered
  5. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    15 Aug in reply to Daniel Plomp
    Link to this post

    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();
5 posts, 1 answered