inquisitive_one
inquisitive_one

Reputation: 1495

Linq to MongoDB Filter

I have a MongoDB collection, listed below:

{ 
    "_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")
}

But when I try to do a filter in my query:

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

I get the following error:

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

What should I be doing to get MongoDB to handshake with LINQ?

Upvotes: 2

Views: 3866

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

Use a strongly-typed collection when using LINQ queries:

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"));

Upvotes: 2

Related Questions