2014-09-29 31 views
0

我的文档结构包含一个字段作为Bson数组。C#Linq驱动程序到mongoDB - Bson数组

我想从集合中获取所有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阵列迭代得到的一切作为字符串列表的第一个值。

回答

2

您在TermMonitorIds集合上使用索引器,因此您在SocialRecord集合中获得了每个集合TermMonitorIds的第二个元素。

你会想,而不是做一个SelectMany像这样:

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

编辑:由于运说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()); 
+1

是啊,我试过了,问题是MongoDB的LinQ在驱动程序不支持的SelectMany。错误 - “SelectMany查询运算符不受支持。” – 2014-09-30 10:10:13

+0

看我的编辑。 :) – Cameron 2014-09-30 13:28:25

0

我用下面的代码来解决这个问题:

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