[英]c# winforms, using async and await in a Search function
I'am trying to search employees by using async and await with button function, but my code doesn't work as my expect and get error like "requires a receiver of type 'IQueryable'".我正在尝试使用 async 搜索员工,并使用按钮 function 等待,但我的代码没有按我的预期工作,并出现“需要类型为‘IQueryable’的接收器”之类的错误。 How should I do if I want to use async await in windows form.
如果我想在 windows 表单中使用异步等待该怎么办。 Here is my code: Thank you in advance!
这是我的代码:提前谢谢!
private async void btnSearch_Click(object sender, EventArgs e)
{
using(db)
{
var employees = db.Analys
.Where(x => x.Status == true)
.Select(a => new { a.UserId, a.FirstName, a.LastName, a.DOB, a.Department, a.DepartmentId })
.AsEnumerable()
.Select(b => new {
UserId = b.UserId,
FirstName = b.FirstName,
LastName = b.LastName,
Age = CalculateAge(b.DOB.ToString()),
Department = b.Department.DepartmenName,
});
var data = await employees.ToListAsync();
if (data!= null)
{
dgvEmployees.DataSource = data;
}
}
}
Can you please add full error text?你能添加完整的错误文本吗? But from the code you've already provided I assume you are using Entity Framework to access your DB, so
AsEnumerable()
should already have materialized the data fetching it from db.但是从您已经提供的代码中,我假设您正在使用实体框架来访问您的数据库,因此
AsEnumerable()
应该已经物化了从数据库中获取它的数据。 I would say that you don't need it here, and first Select
clause too.我会说你在这里不需要它,第一个
Select
子句也是如此。 Try something like this:尝试这样的事情:
var employees = await db.Analys
.Where(x => x.Status == true)
.Select(b => new {
UserId = b.UserId,
FirstName = b.FirstName,
LastName = b.LastName,
Age = CalculateAge(b.DOB.ToString()),
Department = b.Department.DepartmenName,
})
.ToListAsync();
Also the null check should not be needed here, cause EF should return empty collection.此外,此处不需要 null 检查,因为 EF 应该返回空集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.