C# ASP.NET核心实体框架。如何使用子查询编写异步查询?
这是我尝试异步的代码C# ASP.NET核心实体框架。如何使用子查询编写异步查询?,c#,asp.net-core,entity-framework-core,C#,Asp.net Core,Entity Framework Core,这是我尝试异步的代码 public void MarkAsRead(Guid id) { var notificationMessages=DbContext.notificationMessages .Where(nm=>!nm.IsRead&&nm.CreatedDatex.Id==Id).CreatedDate) .ToList(); notificationMessages.ForEach(nm=>nm.IsRead=true); DbContext.SaveChanges(); }
public void MarkAsRead(Guid id)
{
var notificationMessages=DbContext.notificationMessages
.Where(nm=>!nm.IsRead&&nm.CreatedDatex.Id==Id).CreatedDate)
.ToList();
notificationMessages.ForEach(nm=>nm.IsRead=true);
DbContext.SaveChanges();
}
我已经试过了,但是没有用
公共异步任务MarkAsRead(Guid id)
{
var notificationMessages=等待DbContext.notificationMessages
.Where(async nm=>!nm.IsRead&&nm.CreatedDatex.Id==Id).Result.CreatedDate)
.ToListAsync();
notificationMessages.ForEach(nm=>nm.IsRead=true);
等待DbContext.saveChangesSync();
}
主要问题是无法获取子查询字段CreatedDate
错误消息显示:
“DateTime”不包含“GetWaiter”的定义,也不包含
可访问扩展方法“GetAwaiter”接受的第一个参数为
找不到“DateTime”类型(是否缺少using指令或
组件参考?)
传递到
的表达式,其中
由EF简单地转换为SQL,因此无需尝试使其异步
ToListAsync
方法异步执行数据库上的查询,因此应等待:
public async Task MaskAsRead(Guid id)
{
var notificationMessages = await DbContext.NotificationMessages
.Where(nm => !nm.IsRead && nm.CreatedDate < DbContext.NotificationMessages.FirstOrDefault(x => x.Id == id).Result.CreatedDate)
.ToListAsync();
notificationMessages.ForEach(nm => nm.IsRead = true);
await DbContext.SaveChangesAsync();
}
公共异步任务MaskAsRead(Guid id)
{
var notificationMessages=等待DbContext.notificationMessages
.Where(nm=>!nm.IsRead&&nm.CreatedDatex.Id==Id).Result.CreatedDate)
.ToListAsync();
notificationMessages.ForEach(nm=>nm.IsRead=true);
等待DbContext.saveChangesSync();
}
传递到的表达式,其中
通过EF简单地转换为SQL,因此无需尝试使其异步
ToListAsync
方法异步执行数据库上的查询,因此应等待:
public async Task MaskAsRead(Guid id)
{
var notificationMessages = await DbContext.NotificationMessages
.Where(nm => !nm.IsRead && nm.CreatedDate < DbContext.NotificationMessages.FirstOrDefault(x => x.Id == id).Result.CreatedDate)
.ToListAsync();
notificationMessages.ForEach(nm => nm.IsRead = true);
await DbContext.SaveChangesAsync();
}
公共异步任务MaskAsRead(Guid id)
{
var notificationMessages=等待DbContext.notificationMessages
.Where(nm=>!nm.IsRead&&nm.CreatedDatex.Id==Id).Result.CreatedDate)
.ToListAsync();
notificationMessages.ForEach(nm=>nm.IsRead=true);
等待DbContext.saveChangesSync();
}