C# 检查列表对象是否具有特定值
我有一个C# 检查列表对象是否具有特定值,c#,.net,C#,.net,我有一个医生和患者类型的列表。 我需要一种方法来遍历列表,并获取由特定医生治疗的所有患者 //Appointment objects Appointment app1= new Appointment(doctor1, patient1); Appointment app2= new Appointment(doctor2, patient3); Appointment app3= new Appointment(doctor1, patient2); Appointment app4= new
医生
和患者
类型的列表。
我需要一种方法来遍历列表,并获取由特定医生治疗的所有患者
//Appointment objects
Appointment app1= new Appointment(doctor1, patient1);
Appointment app2= new Appointment(doctor2, patient3);
Appointment app3= new Appointment(doctor1, patient2);
Appointment app4= new Appointment(doctor3, patient4);
Appointment app1= new Appointment(zdravnik2, pacient4);
Appointment app1= new Appointment(zdravnik3, pacient2);
Appointment app1 = new Appointment(zdravnik3, pacient5);
Appointment app1 = new Appointment(zdravnik1, pacient6);
Appointment app1 = new Appointment(zdravnik2, pacient4);
Appointment app1 = new Appointment(zdravnik1, pacient4);
//The objects are stored in this list, they are added in the constructor
List<Appointment>AllAppointments=new List<Appointment>();
//Gets all the patients that have been treated by a particular doctor
public List<Patient> HadAppointmentWith(Doctor d)
{
//Dont know how to continue from here on
//var find=AllAppointment.GroupBy(x=>x.Patient)
}
//约会对象
预约app1=新预约(医生1,患者1);
预约app2=新预约(医生2,患者3);
预约app3=新预约(医生1,患者2);
预约app4=新预约(医生3,患者4);
预约app1=新预约(zdravnik2,pacient4);
预约app1=新预约(zdravnik3,pacient2);
预约app1=新预约(zdravnik3,pacient5);
预约app1=新预约(zdravnik1,pacient6);
预约app1=新预约(zdravnik2,pacient4);
预约app1=新预约(zdravnik1,pacient4);
//对象存储在此列表中,并添加到构造函数中
ListalLappoints=新列表();
//获取已由特定医生治疗的所有患者
公共任命名单(d博士)
{
//我不知道如何从现在开始继续下去
//var find=allappoimment.GroupBy(x=>x.Patient)
}
您可以使用Linq扩展方法:
using System.Linq;
public List<Patient> GetPatients(Doctor doctor)
{
return AllAppointments
.Where(appointment => appointment.Doctor == doctor)
.Select(appointment => appointment.Patient)
.Distinct()
.ToList();
}
使用System.Linq;
公众名单病人(医生)
{
退还所有药膏
.Where(预约=>预约.医生==医生)
.选择(预约=>appointment.Patient)
.Distinct()
.ToList();
}
它过滤完整列表,以获得与所需医生匹配的所有医生
接下来,它选择该医生的所有患者
然后在重复的情况下选择不同的患者
我们将Linq查询的IEnumerable
序列结果转换为列表
,以返回方法。这是否回答了您的问题?四个类似的答案和所有收到的投票没有任何评论,有点奇怪,THNIK,因为这个问题太简单,人们跑得快,得到他们想要的点,同时真正的问题,这是值得考虑的,只是忽略了))是的,只是注意到了,不是我。