我对异步编程很陌生。FirstOrDefaultAsync正在抛出一个错误
列表不包含FirstOrDefaultAsync()的定义
有人能解释我做错了什么吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace PatientManagement.Models
{
public class PatientService : IPatientService
{
private List<Patient> patients;
public PatientService()
{
patients = new List<Patient>()
{
new Patient(){ ID = 1, Name = "Akash", Email = "aakash1027@gmail.com" },
new Patient(){ ID = 2, Name = "John", Email = "John@gmail.com" },
new Patient(){ ID = 3, Name = "Mike", Email = "Mike@gmail.com" },
};
}
public async Task<Patient> GetPatient(int id)
{
return await patients.FirstOrDefaultAsync(x => x.ID == id);
}
}
}发布于 2021-06-12 16:20:48
问题是列表中确实没有FirstOrDefaultAsync方法!
异步代码适用于这样的情况,例如,当您跨越边界时,必须等待数据库或其他进程进行工作。您的情况是不同的:您的流程实际上必须在列表中找到此记录。因此,您应该只使用普通的同步FirstOrDefault:
public Patient GetPatient(int id)
{
return patients.FirstOrDefault(x => x.ID == id);
}https://stackoverflow.com/questions/67950639
复制相似问题