如何让患者使用 medicalNumber 而不是 Id?

How to get Patients using medicalNumber not Id?

“我正在编码服务 (REST API),并希望创建通过其 medicalNumber 获取特定患者的操作,就像通过 ID 获取患者一样。我该怎么做?

" 我试过...

//GET: Api/GetPatientsbyMedicalNumber/657847
        public Patients GetPatientsbyMedicalNumber(int medicalNumber)
        {
            var patients = db.Patients.SingleOrDefault(e => e.medicalNumber == medicalNumber);

            if (patients == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);

            return patients;

        }

喜欢...

 //GET: Api/GetPatients/1
        public Patients GetPatients(int id)
        {
            var patients = db.Patients.SingleOrDefault(e => e.Id == id);

            if (patients == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);

            return patients;

        }

通过 id 工作获得耐心 我认为这是工作,因为 Id 是我 table

的关键
public class Patients
    {

        [Key]
        public int Id { get; set; }


        public int medicalNumber { get; set; }


        public string userName { get; set; }


        public int phoneNumber { get; set; }


        public string address { get; set; }


        public string email { get; set; }

        public virtual ICollection<Tests> Tests { get; set; }
    }

但是让病人使用那里的医疗号码不起作用让所有病人..

你能试试这个吗?

public Patients GetPatientsbyMedicalNumber(int medicalNumber)
        {
            Patients patient = db.Patients.Where(e => e.medicalNumber == medicalNumber).FirstOrDefault();

            if (patients == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);

            return patient;

        }

能具体说明问题吗?在 return 之前让患者接受您的咨询?或者你的方法没有 return 一个 Patient 对象?