Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to filter a collection by datetime? #111

Open
miki-nis opened this issue Apr 22, 2017 · 1 comment
Open

How to filter a collection by datetime? #111

miki-nis opened this issue Apr 22, 2017 · 1 comment

Comments

@miki-nis
Copy link

miki-nis commented Apr 22, 2017

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");

@oleg-demkovych
Copy link

oleg-demkovych commented Jun 8, 2017

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");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants