MongoCollection <t>到Collection <t> </t> </t>

时间:2014-05-18 12:48:04

标签: c# .net asp.net-mvc-4 mongodb-.net-driver

我有一个叫做Employee的课程。从我的控制器我传递IEnumerable<Employee>来查看。如何将MongoDB.Driver.MongoCollection<Employee>转换为IEnumerable<Employee>

public ActionResult Index()
{
    var server = MongoServer.Create("mongodb://127.0.0.1");
    var db = server.GetDatabase("employee");
    var employeeCollection = new Collection<Employee>
    {
        new Employee
        {
            EmployeeId = new ObjectId(),
            EmployeeName = "A"
        },
        new Employee
        {
            EmployeeId = new ObjectId(),
            EmployeeName = "B"
        }
    };
    var collection = db.GetCollection<Employee>("employee");

    collection.InsertBatch(employeeCollection);

    return View(collection);
}

1 个答案:

答案 0 :(得分:3)

您可以使用FindAll,这样您就可以枚举整个集合:

return View(collection.FindAll());

您也可以使用AsQueryable来获得相同的结果:

return View(collection.AsQueryable());

但你应该小心这样做。该集合可能包含数百万个文档。