我想列出我的数据中的所有文档,并使用Findasync(csharpdriver 2.2.3)查找所有文档,但它永远不会返回。你能给我一些建议吗? 这是我的代码
public class HomeController : Controller
{
readonly MyVietnamContext Context = new MyVietnamContext();
private List<UserModels> list = new List<UserModels>();
public ActionResult Index()
{
GetUsers().Wait();
return View(list);
}
public async Task GetUsers()
{
var filter = new BsonDocument();
var collection = Context.Collection();
var cursor = await collection.FindAsync(filter);
while (await cursor.MoveNextAsync())
{
var batch = cursor.Current;
list.AddRange(batch);
}
}
}
答案 0 :(得分:0)
将您的代码更改为
public async Task<ActionResult> Index()
{
await GetUsersAsync();
return View(list);
}
public async Task<Context.Collection> GetUsersAsync()
{
var filter = new BsonDocument();
var collection = Context.Collection();
var cursor = await collection.FindAsync(filter);
while (await cursor.MoveNextAsync())
{
var batch = cursor.Current;
list.AddRange(batch);
}
return list;
}