+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

4 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
4 posts, 1 answered