【发布时间】: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