从方法</list>返回<list>值

时间:2012-06-26 11:34:38

标签: c# asp.net entity-framework

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

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,doctorid,约会日期这样的字段,

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

appointments.add(appointment);

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

我不知道如何做到这一点,任何人都可以帮助我,在此先感谢

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:1)

您可以执行以下操作:

dataEntities.Appointments.Where(a => a.PATIENTID == PatientId)
  .Select (new Appoinment { ... set values from DB element });

答案 2 :(得分:1)

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

https://github.com/AutoMapper/AutoMapper