2014-06-16 93 views
0

我想查询MongoDB以获得Top 5000记录,这会导致以下错误。 我正在使用C#LINQ驱动程序,而TermMonitorIds是BsonArray。Linq查询其中包含列表中的列表

{ “无法确定表达式的序列信息:)x.ToString(”}

public IList<SocialRecord> GetManyBetweenDatesLimited(List<string> termMonitorIds, string[] sources, DateTime fr, DateTime to) 
     { 
      IList<SocialRecord> entities = new List<SocialRecord>(); 

      try 
      { 
       entities = 
        (from e in this.collection.AsQueryable<SocialRecord>() 
        where (e.TermMonitorIds.Any(x => termMonitorIds.Contains(x.ToString()))) && (sources.Contains(e.SocialType)) 
                  && (e.DateCreated.Date >= fr.Date) && (e.DateCreated.Date <= to.Date) 
        select e) 
        .Take(5000) 
        .ToList(); 
      } 
      catch (Exception ex) 
      { 
       Log.Error("Error Message", ex); 
      } 

      return entities; 
     } 

我试图改变列表以BsonArray象下面这样:

BsonArray bArray = new BsonArray(); 
      foreach (var term in termMonitorIds) 
      { 
       bArray.Add(term.ToBson()); 
      } 

静止最后带上如下错误消息:

'/'应用程序中的服务器错误。

无法将字符串值写入BSON文档的根级别。

描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。

异常详细信息:System.InvalidOperationException:String值无法写入到BSON文档的根级别。

+0

您使用的是什么版本的MongoDB? – sloth

+0

MongoDb 2.4.9版本 –

回答

4

LINQ提供程序不支持ToString,因为它不知道如何将其翻译为MongoDB表达式。

我建议你更新termMonitorIds以匹配e.TermMonitorIds返回的预期数据类型。 List<int>/List<Guid>,避免了任何形式的转换(没有它通常更高效)。

+0

LINQ to SQL?我不这么认为:-) – sloth

+0

@sloth是啊意识到我写了*之后我写了它哈哈 – James