[CSHARP-1353] DistinctAsync doesn't allow Sort or ToList Created: 19/Jul/15  Updated: 19/Jul/15  Resolved: 19/Jul/15

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: 2.0.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Lorenz Buchberger Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: Distinct, DistinctAsync
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Visual Studio 2013



 Description   

I'm trying to use DistinctAsync with SortBy or ToListAsync, but it's not possible. Visual Studio does not show thes options when I use it.

List<Job> distListe = await _mongoDb.Jobs
.DistinctAsync(p=>p.Period,p=>p.Monthly)

works, but no Sort, no ToList.

Am I'm doing anything wrong ?



 Comments   
Comment by Robert Stam [ 19/Jul/15 ]

DistinctAsync doesn't have a sort option because the corresponding server command doesn't have a sort option either. You would have to sort the values client side after retrieving them.

DistinctAsync returns a cursor. You can call ToListAsync on the cursor, like this:

var cursor = await collection.DistinctAsync(p => p.Period, p => p.Monthly);
var list = await cursor.ToListAsync();

Generated at Wed Feb 07 21:39:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.