Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET核心实体框架。如何使用子查询编写异步查询?_C#_Asp.net Core_Entity Framework Core - Fatal编程技术网

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();
}