I am working with Couchbase v4.5 and the .NET SDK version 2.3.8. I am in the midst of migrating a view to a N1QL query. The Javascirpt available to the view provided me the flexibility of sorting array members of the document, and subsequently selecting just a single element from the array. So whereas in my view I had:
'Username': doc.Document.LogActions.sort(function(a,b){ return (a.Time > b.Time) })[doc.Document.LogActions.length - 1].Username
...I'm now struggling to accomplish the same via the .NET SDK in a N1QL query. The caveat is that I'm attempting to do this solely in LINQ. I realize that I can use the raw string queries, which offer full N1QL support, but the team lead wants this done via LINQ...unless there's absolutely no way to do it via LINQ.
I've tried the following:
Sorting - Not allowed to sort by a property on an object in an array
Username = doc.Document.LogActions
.OrderByDescending(logAction => logAction.Time)
.FirstOrDefault()
.Username,
Take Just Last - Last not supported
Username = transaction.Document.LogActions
.LastOrDefault()
.Username,
No Sorting - Still returns an array
Username = transaction.Document
.LogActions[transaction.Document.LogActions.Count - 1]
.Username
The only other thing I can think to do is just return the array back to .NET and do the filtering there. However, we have no baseline for how many items will be in the array on average. We'd rather avoid bringing back data we don't need.
Am I relegated to using a raw string query? Ideally I'm trying to find the most recent "log action" from the array, based on the Time
property.