Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am studying mongodb.
the below code is ok.
XML
using (var mongo = new Mongo(connectionstring))
            {
                mongo.Connect();
                var db = mongo.GetDatabase(dbName);
                var connection = db.GetCollection<Customer>();
                  
                Customer customer = connection.FindOne(x => x.CustomerID == "001");
                
                return customer;
            }

But when using Expression,I can not get the result.Why?
XML
using (var mongo = new Mongo(connectionstring))
            {
                mongo.Connect();
                var db = mongo.GetDatabase(dbName);
                var connection = db.GetCollection<Customer>();
                Func<Customer, bool> exp = x => x.CustomerID == "001";               
                Customer customer = connection.FindOne(exp);
                return customer;
            }
Posted
Updated 9-Dec-13 16:15pm
v4
Comments
cyousyun 10-Dec-13 0:58am    
Thanks for your solution.but what i want to do is to pass parameter to findone.as below.
public class MongoDBBase<t> where T:class
{
private static string connectionstring = "Server=127.0.0.1:28013";
private static string dbName = "blog";

public T GetOne(Func<t,bool> func)
{
using (var mongo = new Mongo(connectionstring))
{
mongo.Connect();
var db = mongo.GetDatabase(dbName);
var connection = db.GetCollection<t>();
var t= connection.FindOne(func);
return t;
}
}
}

1 solution

See First Know what to do
In MongoDatabase Format you are using FindOne method from a Mongo Collection
N.B.
connection.FindOne(IMongoQuery query);
It Means query usually a QueryDocument or constructed using the QueryBuilder
For this suggestion Use IMongoQuery

  IMongoQuery query = Query.EQ("CustomerID", "001");
  Customer customer = connection.FindOne(query);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900