I'm trying to use the predicate builder to get the LastVerifiedDate ordered in descending order by using the PredicateBuilder
in LinqKit
So far It didn't work out. Here's what I have tried so far:
var predicate = PredicateBuilder.New<Item>();
predicate.And(f => OrderByDescending(f.LastVerfiedDate)); // Not working code
Im okay to add to Cosmos Query aswell like below:
var response = _client.CreateDocumentQuery<T>(_tools.GetCollectionUri<T>(),
new FeedOptions
{
PartitionKey = new PartitionKey(partitionKey), MaxItemCount = requestCommand.PageSize,
RequestContinuation = requestCommand.Token
})
var docQuery = response.Where(predicate).OrderByDescending(f.LastVerfiedDate); // OrderByDescending not available
Question:
Can this be achievable using CreateDocumentQuery?
Basically I need to fetch data in descending order based on LastVerifiedDate
attribute. If so can someone provide code sample which I can try out?
I was basically able to achieve ordering data based on Expression via the below code. Posting it here for anyones reference.
My use case was to order the data based on DateTime
parameter called LastVerifiedDate
which I have from data fetched CreateDocumentQuery. I was able to achieve like below :
public async Task<PagedResults<TResult>> GetPagedAsync<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, DateTime>> orderedByDateAscending,
PagedCommand<T> requestCommand, string partitionKey = null)
{
var response = string.IsNullOrWhiteSpace(partitionKey)
? _client
.CreateDocumentQuery<T>(_tools.GetCollectionUri<T>(),
new FeedOptions
{MaxItemCount = requestCommand.PageSize, RequestContinuation = requestCommand.Token})
: _client
.CreateDocumentQuery<T>(_tools.GetCollectionUri<T>(),
new FeedOptions
{
PartitionKey = new PartitionKey(partitionKey), MaxItemCount = requestCommand.PageSize,
RequestContinuation = requestCommand.Token
});
var docQuery = response.Where(predicate).OrderBy(orderedByDateAscending); // Pass it like this to OrderBy method
var total = requestCommand.TotalRecords;
if (requestCommand.Token == null && !requestCommand.HasMore)
{
total = docQuery.Count();
}
var query = docQuery.AsDocumentQuery();
var list = await query.ExecuteNextAsync<TResult>();
var pagedResult = new PagedResults<TResult>(requestCommand.PageSize, total, list.ResponseContinuation,
query.HasMoreResults, requestCommand.PageIndex + 1, list.ToList());
return pagedResult;
}
Invoking the above method like below :
Expression<Func<StartPoints, DateTime>> orderByExpression = e=>e.LastVerfiedDate;
var results = await _myQueryRepository.GetPagedAsync<ItemDto>(predicate, orderByExpression, request);