簡體   English   中英

C#Linq驅動程序到mongoDB-Bson數組

[英]C# Linq Driver to mongoDB - Bson Array

我的文檔結構包含一個字段作為Bson Array。

我想從所有Bson數組值作為字符串列表的集合中獲取所有記錄。

我知道下面的代碼是錯誤的,因為我只是得到Bson數組的第一個值

 public List<String> GetRecordsfromTime(DateTime dateTime)
            {
                return _collection.AsQueryable<SocialRecord>().Where(x => x.DateCreated > dateTime).Select(x => x.TermMonitorIds[1].ToString()).ToList();
            }

有人可以知道如何遍歷這些bson數組以獲取所有內容作為字符串列表。

您正在使用您的索引TermMonitorIds集合,所以你得到的每一個集合的第二個元素TermMonitorIds您在SocialRecord集合。

您將需要像這樣執行SelectMany

return _collection.AsQueryable<SocialRecord>()
                  .Where(x => x.DateCreated > dateTime)
                  .SelectMany(x => x.TermMonitorIds.ToString())
                  .ToList();

編輯:由於OP已經說過MongoDB不允許SelectMany查詢運算符。

// Evaluate the query
var socialRecords = _collection.AsQueryable<SocialRecord>()
                               .Where(x => x.DateCreated > dateTime)
                               .ToList();

// Return desired results.
return socialRecords.SelectMany(x => x.TermMonitorIds.ToString());

我用下面的代碼來解決這個問題:

 return _collection.AsQueryable<SocialRecord>()
                           .Where(x => x.DateCreated > dateTime)
                           .Select(z => z.TermMonitorIds.Select(x => x.ToString()).ToList())
                           .ToList()
                           .SelectMany(x => x)
                           .ToList();

暫無
暫無

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

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