order by custom field in newsitem - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

order by custom field in newsitem

  • order by custom field in newsitem
  • 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
  • i guess its not possible then
  • 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
  • cheers ivan, worked a treat, me being a tool (again), clue was in the error, more haste = less speed! but bah to nullable DateTimes....
  • 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

  • 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
     
  • It worked. Thank you