我有一个叫做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);
}
答案 0 :(得分:3)
您可以使用FindAll
,这样您就可以枚举整个集合:
return View(collection.FindAll());
您也可以使用AsQueryable
来获得相同的结果:
return View(collection.AsQueryable());
但你应该小心这样做。该集合可能包含数百万个文档。