我想使用异步等待功能按年龄分组。无需等待aync即可正常工作,但我要使用asyn等待。如果我从db.Patient.ToList()中的u中排除第一行中的ToList()...则CalculateAge函数会出现问题错误LINQ实体在方法CalculateAge(system.String)...中无法识别。这就是为什么在那里使用它
此工作正常,无需等待异步
private void btnByAge_Click(对象发送者,EventArgs e){
var result = (from u in db.Patient.ToList() //
join a in db.Analys on u.PatientId equals a.PatientId
where a.Status == Active
group u by CalculateAge(u.DOB.ToString()) into g
select new
{
Age = g.Key,
Amount = g.Count(),
}).Tolist();
if (result != null)
{
dgvResult.DataSource = result;
}
}
但是在异步等待时不行
private async void btnByAge_Click(object sender, EventArgs e)
{
var result = (from u in db.Patient.ToListAsync()
join a in db.Analys on u.PatientId equals a.PatientId
where a.Status == Active
group u by CalculateAge(u.DOB.ToString()) into g
select new
{
Age = g.Key,
Amount = g.Count(),
});
var data = await result.ToListAsync();
if (data != null)
{
dgvResult.DataSource = data;
}
}
我得到的错误是错误CS1936找不到源类型'任务>'的查询模式的实现。找不到“加入” ...我什至使用过Using.System.Data.Linq,但是同样的问题。请帮助,提前谢谢!
解决该错误所需要做的就是将await
放在db.Patient.ToListAsync()
之前。但是最好不要执行ToList
,以便可以在数据库中完成连接。
var result = (from u in db.Patient
join a in db.Analys on u.PatientId equals a.PatientId
where a.Status == Active
select u.DOB)
.AsEnumerable()
.GroupBy(x => CalculateAge(x.toString())
.Select(grp => new
{
Age = grp.Key,
Amount = grp.Count(),
});
var data = await result.ToListAsync();
if (data != null)
{
dgvResult.DataSource = data;
}
这将进行连接并根据Status进行过滤,并在DB中选择DOB,然后在内存中将计算年龄和组并获取每个的计数。但是一个更好的主意是在数据库中进行年龄计算。我不确定CalculateAge
的确切功能,但是有可能。
var now = DateTime.UtcNow(); // or .Now() if you don't store UTC in the DB
var result = (from u in db.Patient
join a in db.Analys on u.PatientId equals a.PatientId
where a.Status == Active
let age = now.Year - u.DOB.Year -
(u.DOB.Month < now.Month ||
(u.DOB.Month == now.Month && u.DOB.Day < now.Day))
? 1 : 0
group u by age into g
select new
{
Age = grp.Key,
Amount = grp.Count(),
};
var data = await result.ToListAsync();
if (data != null)
{
dgvResult.DataSource = data;
}
这将通过从当年减去DOB年,然后如果当前日在DOB的月和日之前减去1来确定年龄。