【问题标题】:How to convert multiple join associated with aggregate function Linq to sql C#?如何将与聚合函数 Linq 关联的多个联接转换为 sql C#?
【发布时间】:2014-05-29 22:53:14
【问题描述】:

我想使用 Linq to sql 编写以下查询? 我试着去做,但我是 Linq 的初学者,我迷失了,请任何帮助或任何有助于解决这个问题的文章提示?

select (s.fname + '' + s.lname ) as doctor_name  , appointment_id , [date/time] , price , type ,                                                                                                                                           
patient_id
from staff s
,
(select  appointment_id,[date/time] , price , apptype.type , patient_id , [doctor-id] from     

AppointmentType apptype ,
(  select * 
from Appointments app
where app.appointment_id = 
(
    select Max(appointment_id) as lastApp
    from Appointments app1
    where app1.patient_id = app.patient_id and app.patient_id=10
) 
) as rr
where apptype.id = rr.type
) as dd
where dd.[doctor-id] = s.id

请给点建议,谢谢 :)

【问题讨论】:

  • 你能告诉我们你到目前为止写的代码吗?至少我们会知道你的实体的名称和属性......

标签: c# sql-server linq linq-to-sql linq-to-objects


【解决方案1】:
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                      var x = from apptype in db.AppointmentTypes
                                    from rr in (
                                        (from app in db.Appointments
                                           where
                                           app.appointment_id == 
                                            (from app1 in db.Appointments
                                              where
                                              app1.patient_id == app.patient_id &&
                                              app.patient_id == patient.Patient_id
                                            select new {
                                              app1.appointment_id
                                            }).Max(p => p.appointment_id)
                                        select new {
                                          app
                                        }))
                                 where
                                 apptype.id == rr.app.type
                                    select new {
                                        apptype.id,
                                   //   apptype.appointment_id,
                                    ///// take ***  Column1 =  rr.app.date_time ,
                                      apptype.price,
                                      apptype.type,
                                      rr.app.patient_id      //   apptype.patient_id,
                                     // Column2 = (apptype.doctor - apptype.id)
                                    };

【讨论】:

    猜你喜欢
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    相关资源
    最近更新 更多