Question

folowing situation: I have a list of Users, evry one have a field with a List of Comments.

User1: {
      ...
      Id : 'xxxx',
      Comment : [{
                   ...
                   Status : 1
                }
                ,{
                   ...
                   Status : 0
                }]
}

I am looking for a Mongo c# Query that select all Comments with status 1 of all users in the DB Collection.

sry for bad English.

thx

Was it helpful?

Solution 2

thx.

I slove it like this:

var query1  =  Query.EQ("Comments.Status", 1)
IEnumerable<Comment> comments = Collection.Distinct<Comment>("Comments", query1).Where(x => x.Status == 1);

comments .toList() // <= list of Comments with Status 1

if someone has a better solution please post it.

Thx again,

Benjamin

OTHER TIPS

Let's assume you have the following classes that contain the serialized values for your collection:

public class User1
{
    public string Id { get; set; }

    public Comment[] Comments { get; set; }
}

public class Comment
{
   public int Status { get; set; }
}

Then the query should be something like:

var query =
    collection.AsQueryable<User1>().SelectMany(user => user.Comments.Where(com=>com.Status == 1));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top