繁体   English   中英

Linq groupby orderby and join together

[英]Linq groupby orderby and join together

我有两个表 Appointments 和 Patients,考虑他们以以下方式拥有数据:请在

附加图片

以上是我的数据表。 我的情况是我必须让患者了解特定的医生。 下面的查询有效,但没有给出不同的结果。 我不止一次获得相同的患者数据,我可以在检索结果后使用 distinct,但我必须在行查询本身中执行操作(在数据库本身中)

from a in dbContext.Appointments
where a.doctorid == mydoctorid 
join p in dbContext.Patients on a.patientid equals p.patientid
order by p.name

updated code which led to exception

(from p in this.dbContext.Patients
join b in ( from a in this.dbContext.Appointments
            join p in this.dbcontext.Patient on a.Patientid equals p.id
            where a.doctorid == doctorid
            group a by a.Patientid into pg)
            on p.Patientid equals b.FirstOrDefault().Patientid
            order by p.Name
            select new { p.Patientid, p.Name }).ToList()

final code which i tried:

            (from p in this.m_dbContext.Patient
            join b in (from a in this.m_dbContext.Appointments
            join p in this.m_dbContext.Patient on a.Patientid equals 
            p.Patientid
            where a.Doctorid == doctorid && a.Clinicid == clinicid
            group a by a.Patientid)
            on p.Patientid equals b.FirstOrDefault().Patientid
            orderby p.Name
            select new
            {
              p.Patientid,
              p.Clinicid,
              p.Name,
              p.Mobilenumber,
              p.Gender,
              p.Dob,
              p.Age,
              p.Address,
              p.City,
              p.State,
              p.Pincode
           }).ToList().Count();

例外:

LINQ 表达式 'FirstOrDefault(GroupByShaperExpression: KeySelector: a.patientid, ElementSelector:EntityShaperExpression: EntityType: Appointments ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember IsNullable: False)' 无法翻译。 以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。 有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038

架构命名约定略有不同,但您可以通过以下查询获得所需的 output。

此查询使用 group by 获取所有唯一患者,然后通过子查询获取名称。

考虑将 p.Id 更改为 p.PatientId

from p in dbContext.Patients
join b in (from a in dbContext.Appointments
      join p in dbContext.Patients on a.PatientId equals p.Id
      where a.DoctorId == mydoctorId
      group a by a.PatientId)
on p.Id equals b.FirstOrDefault().PatientId
select new {p.Id, p.Name}


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM