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

Forums / Developing with Sitefinity / order by custom field in newsitem

order by custom field in newsitem

6 posts, 1 answered
  1. tim
    tim avatar
    27 posts
    Registered:
    12 Oct 2010
    12 Sep 2012
    Link to this post
    Hi

    Ive searched but cant seem to find any answer to this and its driving me nuts now. All I want to do is order my newsitem list by a custom field called ArticleDate, this is a DateTime which was necessary so they could be ordered in a specific way. I have found some example that nearly get there but I am getting errors.

    I am attempting to use
    .OrderByDescending(i => i.GetValue<DateTime>("ArticleDate"))
    but am getting the following error :
    Database mapped field uses different type 'System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
    Parameter name: methodCallExpression
    Actual value was i.FieldValue("ArticleDate").
    if I dont sepcify <DateTime> then I get this error:
    NonGeneric method 'object GetValue(string fieldName)' is not supported in LINQ queries. Use generic 'TValue GetValue<TValue>(string fieldName)' instead.

    Is it possible to sort on a custom field in a newsitem ?

    Thaks
    tim
  2. tim
    tim avatar
    27 posts
    Registered:
    12 Oct 2010
    14 Sep 2012
    Link to this post
    i guess its not possible then
  3. Ivan Pelovski
    Ivan Pelovski avatar
    43 posts
    Registered:
    24 Sep 2012
    14 Sep 2012
    Link to this post
    Hi tim,

    The error you receive suggests using a nullable DateTime:
    .OrderByDescending(i => i.GetValue<DateTime?>("ArticleDate"))
    I have tested the code on my machine and it works.

    Regards,
    Ivan Pelovski
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  4. tim
    tim avatar
    27 posts
    Registered:
    12 Oct 2010
    14 Sep 2012
    Link to this post
    cheers ivan, worked a treat, me being a tool (again), clue was in the error, more haste = less speed! but bah to nullable DateTimes....
  5. Arun
    Arun avatar
    10 posts
    Registered:
    03 Nov 2011
    16 Oct 2014
    Link to this post

    Hi,

     

    I have a custom field created in News module and i want to filter the news items by year(2013 or 2012), And I am trying the below

    var NewsItemsInCategory = newsMgr.GetNewsItems().Where(p => p.Status == ContentLifecycleStatus.Live && p.GetValue<DateTime?>("NewsDate").Value.Year == strYear);

    var NewsItemsInCategory = newsMgr.GetNewsItems().Where(p => p.Status == ContentLifecycleStatus.Live && p.GetValue<DateTime>("NewsDate").Year == strYear);

     

    Please help.

     

    Thanks,

    Arun

  6. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    29 Nov 2016
    21 Oct 2014
    Link to this post
    Hi Arun,

    I have test your scenario with the following code:
    var newsMgr = NewsManager.GetManager();
    var strYear = 2014;
    var fieldValue = newsMgr.GetNewsItems().FirstOrDefault().GetValue("NewsDate"); // Just for info
    var NewsItemsInCategory = newsMgr.GetNewsItems().Where(p => p.Status == ContentLifecycleStatus.Live && p.GetValue<DateTime?>("NewsDate").Value.Year == strYear);
    and it works fine.

    The custom field is of type Date and Time and is displayed as Text box.

    Do you have the same settings?

    Regards,
    Svetoslav Manchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
6 posts, 1 answered