ODataAngularResources
ODataAngularResources copied to clipboard
How to filter a collection by datetime?
I have an endpoint that returns Post objects which have collections of PostComment objects, both of which have property LastModifiedDateTime of type DateTime. I am trying to query this endpoint by either Post/LastModifiedDateTime OR PostComment/LastModifiedDateTime with operator ">" and a specific datetime.
The URL query that accomplishes this looks like: https://localhost:44302/odata/Posts?$expand=PostResources,PostComments&$filter=(LastModifiedDateTime gt 2017-04-21T05:54:15.167Z) or (PostComments/any(c: c/LastModifiedDateTime gt 2017-04-21T05:54:15.167Z))&$orderby=CreatedDateTime desc
I am having trouble writing Predicates that accomplish this in angular code.
var predicates = []; var unreadPost = new $odata.Predicate("LastModifiedDateTime", ">", new $odata.Value(syncService.postSeenDateTime, "DateTimeOffset")); predicates.push(unreadPost);
// how to write predicate for 'any' of the nested LastModifiedDateTime gt than a date here ?
filter = $odata.Predicate.or(predicates);
var posts = context.odata() .expand("PostResources") .expand("PostComments") .filter(predicates ) .orderBy("CreatedDateTime", "desc");
var start = new $odata.Predicate('LastModifiedDateTime', '>=', new $odata.Property(startDate));
var end = new $odata.Predicate('LastModifiedDateTime', '<=', new $odata.Property(endDate));
var combination = $odata.Predicate.and([start, end]);
context.odata()
.expand("PostResources")
.expand("PostComments")
.filter(combination)
.orderBy("CreatedDateTime", "desc");