문제

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

도움이 되었습니까?

해결책 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

다른 팁

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));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top