0

I have the following document structure in mongoDb:

mongoDB document structure

I want to query it from an asp.net C# app using linq. I would like a distinct list of all the t values. If I run this code I get a list of t values for all of my documents and the value of t is repeated over documents.

var query = from m in collection.AsQueryable()
                        select m.t;

I therefore want a distinct list of t. I amended my code to the following but nothing is returned and there is no error message.

var query = (from p in collection.AsQueryable()
                        select p.t).Distinct();

What am I doing wrong?

1 Answer 1

4

The thing is that Distinct() returns IQueryable<T> which represents the database operation. To materialize that you need to run .ToList(), the rest of your code is fine, try:

var query = (from p in collection.AsQueryable()
                    select p.t).Distinct();
var data = query.ToList();
Sign up to request clarification or add additional context in comments.

5 Comments

this answer is actually right, BsonDocument already implements IEquatable<BsonDocument>
@mickl - thanks. Is there anyway to speed it up as its been well over a minute and its still populating data. My db has over 3 million documents and there are approx 3000 distinct values of t.
@Silentbob do you have an index on "t" field ?
@mickl yes i do
@Silentbob then I suppose you need to cache this distinct list somehow, maybe in-memory, maybe in a separate collection that can be recalculated periodically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.