简体   繁体   English

Mongo c# 驱动程序查询(选择子字段)

[英]Mongo c# Driver Query (Select subfields)

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.我正在寻找一个 Mongo c# 查询,它选择数据库集合中所有用户的状态为 1 的所有评论。

sry for bad English. sry 英语不好。

thx谢谢

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));

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本杰明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM