【问题标题】:Linq to MongoDB FilterLinq 到 MongoDB 过滤器
【发布时间】:2016-06-13 23:44:21
【问题描述】:

我有一个 MongoDB 集合,如下所列:

{ 
    "_id" : ObjectId("001"), 
    "ticker" : "MSFT=US", 
    "exchange" : "OTC", 
    "localtick" : "MSFT", 
    "compname" : "Microsoft", 
    "currency" : "USD", 
    "insertedtime" : ISODate("2016-06-13T23:10:09.341+0000")
}
{ 
    "_id" : ObjectId("002"), 
    "ticker" : "TSLA=CA", 
    "exchange" : "TSX", 
    "localtick" : "TSLA", , 
    "compname" : "Tesla", 
    "currency" : "CAD", 
    "insertedtime" : ISODate("2016-06-13T23:10:09.809+0000")
}

但是当我尝试在我的查询中进行过滤时:

var documents = collection.AsQueryable()
            .Where(c => c["ticker"].ToString().Contains("=CA"));

我收到以下错误:

Unsupported filter: {document}{ticker}.ToString().Contains("=CA").

我应该怎么做才能让 MongoDB 与 LINQ 握手?

【问题讨论】:

    标签: c# mongodb linq mongodb-query mongodb-csharp-2.0


    【解决方案1】:

    在使用 LINQ 查询时使用强类型集合:

    public class Test
    {
        public ObjectId _id;
        public string ticker;
        public string exchange;
        public string localtick;
        public string compname;
        public string currency;
        public DateTime insertedtime;
    }
    
    var query = db.GetCollection<Test>("test")
        .AsQueryable()
        .Where(c => c.ticker.Contains("=CA"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 2021-03-19
      • 1970-01-01
      相关资源
      最近更新 更多