1

我需要从我的桌子上为特定患者获取所有预约,为此我尝试过,

public List<Appoinment> GetAppoinments()         //here Appointment is a class.
    {
        List<Appoinment> appointments = null;

        foreach (DataObject.Appointment appointment in
                   dataEntities.Appointments.Where(a => a.PATIENTID == PatientId))
        {

        }

        return appointments;
    }

在我的约会类和表中,我有像 PatientId、医生、约会日期这样的字段,

在这里,在我尝试过的 foreach 循环中,

appointments.add(appointment);

它的thorwing错误,它不能自动从数据库表约会转换为类约会,对吧。

我不知道如何做到这一点,任何人都可以在这里帮助我,提前谢谢

4

4 回答 4

3
public List<Appointment> GetAppointments()      
{
    return dataEntities.Appointments
                       .Where(a => a.PATIENTID == PatientId)
                       .Select(a => new OtherNamespace.Appointment 
                                    {
                                        Id = a.Id,
                                        Name = a.Name,
                                        // etc.
                                    })
                       .ToList();
}
于 2012-06-26T11:39:36.073 回答
1

您将 Data.Appointment 转换为 Your.Appointment。

你可以做这样的事情。

My.Appoint resultAppointment = new My.Appointment();
resultAppointment.Id = dataAppointment.Id

但更好的方法是对数据点进行投影。生成的 sql 查询将只获取必要的字段

public List<Appoinment> GetAppoinments()         //here Appointment is a class.
    {
        return dataEntities.Appointments.Where(a => a.PATIENTID == PatientId))
                                        .Select(a => new Appointment() { Prop1 = a.Prop1})
                                        .ToList();

    }
于 2012-06-26T11:40:53.957 回答
1

我同意上面的解决方案....但是,如果您必须经常编写这种映射代码...即将一个对象的属性复制到另一个对象的代码,那么您可以考虑使用 Automapper

https://github.com/AutoMapper/AutoMapper

于 2012-06-26T11:43:32.753 回答
1

您可以执行以下操作:

dataEntities.Appointments.Where(a => a.PATIENTID == PatientId)
  .Select (new Appoinment { ... set values from DB element });
于 2012-06-26T11:39:43.937 回答