使用c#.net从mongodb获取数据时出错

问题描述 投票:0回答:2
string connectionString = "mongodb://10.10.32.125:27017";
        MongoClientSettings settings = MongoClientSettings.FromUrl(new 
       MongoUrl(connectionString));
        MongoClient mongoClient = new MongoClient(settings);
        var Server = mongoClient.GetDatabase("mongovaibhav");
        var collection = Server.GetCollection<employee>("mongov");


        employee cd = new employee();
        foreach (employee employess in collection.Find({ fname: "vaibhav" }) )
        {

        }

我在foreach循环和MongoCollection得到错误。我怎样才能获得数据列表? This error is showing

c# mongodb
2个回答
0
投票

你创建了一个filter

var filter = Builders<employe>.Filter.Eq("fname", "vaibhav") 

然后,您可以使用filter访问您的数据

第一种选择

foreach (employee employess in 
collection.Find(filter).ToListAsync().Result )
{
  //do your computation
}

第二种选择

var employes = await collection.Find(filter).ToListAsync();
foreach(employe in employes)
{
  //do your computation
}

0
投票
 string connectionString = "mongodb://10.10.32.125:27017";
        MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
        MongoClient mongoClient = new MongoClient(settings);
        var Server = mongoClient.GetDatabase("mongovaibhav");
        var collection = Server.GetCollection<employee>("mongov");
        var filter = Builders<employee>.Filter.Eq("fname", "vaibhav");
        List<employee> emp = new List<employee>();
       foreach (var emp1 in collection.Find(filter).ToListAsync().Result )
        {
            emp.Add(emp1);
        }
        GridView1.DataSource = emp;
        GridView1.DataBind();

我得到解决方案谢谢大家和特别的@edkeveked

© www.soinside.com 2019 - 2024. All rights reserved.