[英]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.