我不习惯使用 LINQ 编写 JOIN 语句。我正在尝试将这个旧查询转换为 LINQ Lambda 语法,并努力了解如何进行它。任何帮助将不胜感激。
SELECT p.id, p.code, u.email, b.date
FROM
Patients p
LEFT JOIN Users u
ON u.patientid =p.id and u.id=p.primarycontactid
INNER JOIN BillingSettings b ON b.PracticeId = p.id and b.isDue=1
WHERE p.status=1
AND p.appointmentdate > now() and p.appointmentdate < now() + 90
and b.expirationdate < now()
order by p.id
我在没有编译器的情况下写了这篇文章,所以它可能是错误的,但通常你的答案如下。
var now = DateTime.Now();
var query =
from p in db.Patients
join u in db.Users on new { patientid = p.id, p.primarycontactid} equals { u.patientid, primarycontactid = u.id}
from u in userPatient.DefaultIfEmpty()
join b in db.BillingSettings on b.PracticeId equals p.id
where p.status == 1 &&
p.appointmentdate > now && p.appointmentdate < now.AddDays(90) &&
(b == null || (b.isDue == 1 && b.expirationdate < now))
orderby p.id
select new
{
p.id,
p.code,
email = u?.email
date = b?.date
};