【问题标题】:Linq groupby orderby and join togetherLinq groupby orderby 和 join 一起
【发布时间】:2020-04-29 06:57:19
【问题描述】:

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

中找到 db 架构

以上是我的数据表。我的情况是我必须让患者了解特定的医生。下面的查询有效,但没有给出不同的结果。我不止一次获得相同的患者数据,我可以在检索结果后使用 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, 元素选择器:实体形状表达式: EntityType:约会 值缓冲区表达式: ProjectionBindingExpression:EmptyProjectionMember IsNullable: 假 )' 无法翻译。以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。请参阅https://go.microsoft.com/fwlink/?linkid=2101038 了解更多信息。

【问题讨论】:

  • 您是否尝试在查询中添加.Distinct()
  • 不同的工作,但它是客户端,我必须在数据库本身执行...
  • 每次预约都会添加一个条目,我错误地添加了相同的时间,实际上不同的时间会预约并添加条目
  • select 将是 p.name 单独,distinct 就像从数据库中获取(例如我将获取所有记录,包括重复记录),然后只能应用 distinct,我需要过滤在 linqq 本身中,group by 将是一个选项,但我无法获得正确的语法
  • @HariKrishnanVasanthamaniRag - 是什么让您认为 .Distinct() 是客户端?

标签: c# .net entity-framework linq .net-core


【解决方案1】:

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

此查询使用 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}


【讨论】:

  • 会检查并通知您,抱歉刚刚检查@Muhammad
  • 我认为你会发现这样做是多余的 into pg select pg - 你应该可以不用这些。
  • 但是select new {p.Id, p.Name}可以换成select p
  • @Enigmativity 是的,你是对的。我也更新了我的答案
  • 它抛出查询翻译异常@Muhammad,异常说 FirstOrDefault() 无法翻译
猜你喜欢
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2013-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多