返回加入表

时间:2011-11-15 10:39:02

标签: c# wcf linq entity-framework linq-to-entities

我为我的域上下文输入了这个linq查询,但为什么它无法在tblaptmt中检索两个表匹配的学生ID的数据?

    public IQueryable<StudentViewAppointment> StudentViewAppointments(string StuId)
    {
        //IQueryable<StudentViewAppointment> studentViewAppointments =
        //from aptmt in this.ObjectContext.tblaptmts join ch in
        //aptmt.consultationID equals ch.consultationID where aptmt.studentID == StuId
        //select aptmt;
        //return studentViewAppointments as IQueryable<StudentViewAppointment>;

        return ObjectContext.tblaptmts.Where(a => a.studentID == StuId).Join
        (ObjectContext.tblConsultationHours, a => a.consultationID, 
        ch => ch.consultationID, (a, ch) =>
            new StudentViewAppointment()
                {
                    AppointmentId = a.aptmtID,
                    Apremark = a.apremark,
                    APstatus = a.apstatus,
                    APsubject = a.apsubject,
                    ConsultationId = a.consultationID,
                    Day = ch.cday,
                    StartTime = (DateTime)ch.cstartTime,
                    EndTime = (DateTime)ch.cendTime,
                    LectureId = ch.lecturerID,
                    StudentId = a.studentID
                });
    }

域名服务类。 CS

public partial class StudentViewAppointment
{
    [Key]
    public int AppointmentId { get; set; }
    public string Apremark { get; set; }
    public string APsubject { get; set; }
    public string APstatus { get; set; }
    public int ConsultationId { get; set; }
    public string StudentId { get; set; }
    public string Venue { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
    public string LectureId { get; set; }
    public string Day { set; get; }
}

域服务类Metadata.cs

dgSlot.ItemsSource = context.StudentViewAppointments;
context.Load(context.StudentViewAppointmentsQuery("TP123123"));

Datagrid.xaml.cs

1 个答案:

答案 0 :(得分:3)

return (from a in ObjectContext.tblaptmts
        join ch in ObjectContext.tblConsultationHours 
             on  a.consultationID equals ch.consultationID
        where  a.studentID == StuId
        select new StudentViewAppointment() 
                  {                       
                       AppointmentId = a.aptmtID,
                       Apremark = a.apremark,
                       -----
                       -----
                  });