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"))
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").
NonGeneric method 'object GetValue(string fieldName)' is not supported in LINQ queries. Use generic 'TValue GetValue<
TValue
>(string fieldName)' instead.
i guess its not possible then
Hi tim,
The error you receive suggests using a nullable DateTime:
.OrderByDescending(i => i.GetValue<DateTime?>(
"ArticleDate"
))
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);
It worked. Thank you