簡體   English   中英

Mongo c# 驅動程序查詢(選擇子字段)

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

以下情況:我有一個用戶列表,每個用戶都有一個帶有評論列表的字段。

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

我正在尋找一個 Mongo c# 查詢,它選擇數據庫集合中所有用戶的狀態為 1 的所有評論。

sry 英語不好。

謝謝

假設您有以下包含集合的序列化值的類:

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

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

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

那么查詢應該是這樣的:

var query =
    collection.AsQueryable<User1>().SelectMany(user => user.Comments.Where(com=>com.Status == 1));

謝謝。

我喜歡這樣:

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

如果有人有更好的解決方案,請發布。

再次感謝,

本傑明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM