【问题标题】:Check if list object has certain value检查列表对象是否具有一定的价值
【发布时间】:2019-11-04 00:50:29
【问题描述】:

我有一个 doctorpatient 类型的列表。 我需要一种方法来遍历列表并获取所有已由特定doctor 治疗的患者。

//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)
}

【问题讨论】:

  • 这能回答你的问题吗? C# Linq where clause as a variable
  • 四个类似的答案,都被否决了,没有任何评论
  • 是的有点奇怪
  • 我想是因为这个问题太简单了,人们跑得很快就能得到他们想要的分数,而真正值得考虑的问题却被忽略了)))
  • 是的,刚刚注意到,不是我

标签: c# .net


【解决方案1】:

您可以使用 Linq 扩展方法:

using System.Linq;

public List<Patient> GetPatients(Doctor doctor)
{
  return AllAppointments
         .Where(appointment => appointment.Doctor == doctor)
         .Select(appointment => appointment.Patient)
         .Distinct()
         .ToList();
}

它过滤完整列表以获取与所需医生匹配的所有医生。

接下来会选择该医生的所有患者。

然后在重复的情况下选择不同的患者。

并且我们将 Linq 查询的IEnumerable&lt;&gt; 序列结果转换为List&lt;Patient&gt; 用于方法的返回。

【讨论】:

    猜你喜欢
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2017-06-23
    • 2020-01-02
    • 2020-05-12
    • 2021-09-18
    • 2019-07-04
    相关资源
    最近更新 更多