我有一个 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 握手?
最佳答案
使用 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"));
关于c# - Linq to MongoDB 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37800826/