EF core 中用lambda表达式和Linq的一些区别

时间:2024-09-18 09:35:02

转眼一看,又过了10几天没有写博客了,主要还是没有什么可以写的,因为遇到的问题都不是很有价值。不过最近发现用lambda表达式,比用Linq的代码量会少一些,而且也方便一些。不过两者都差不多,相差不是特别大。

在EF core中,表和表之中都有对应的关系,例如一对一,一对多,还有多对多。要实现一对一,一对多加上主外键,实现多对多则需要加上一个中间表,这样结构清晰,维护也非常的方便。

那我们在使用EF core 势必要用到非常多的跨表查询,来看看linq是怎样查找数据库中的信息的:

var query = from a in db.ExaminationOrderInfo
join b in db.ExaminationOrganization on a.OrganizationId equals b.OrganizationId
join c in db.Customerinfo on a.CustomerId equals c.CustomerId
join d in db.Supplierinfo on a.OrganizationId equals d.OrganizationId
where (a.IsDelete == )
&& (appointmentResult == null || a.AppointmentResult == appointmentResult)
&& (isPaperReport == null || a.IsPaperReport == isPaperReport)
&& (examinationStatus == null || a.ExaminationStatus == examinationStatus)
&& (name == null || a.Name == name)
&& (id == null || a.IdCard == id)
&& (organizationName == null || b.Name == organizationName)
&& (customer == null || c.Name == customer)
&& (supplierName == null || d.Name == supplierName)
orderby a.AppayTime
select new ExaminationOrderInfoDto
{
Type = a.Type == ? "取消预约" : "预约",
IdCard = a.IdCard,
Name = a.Name,
StartTime = a.StartTime.ToString(),
EndTime = a.EndTime.ToString(),
AppayTime = a.AppayTime,
ExaminationTime = a.ExaminationTime.ToString(),
AppointmentResult = a.AppointmentResult == ? "成功" : a.AppointmentResult == ? "失败" : "待处理",
ExaminationStatus = a.ExaminationStatus == ? "已登记" : "未登记",
IsPaperReport = a.IsPaperReport == ? "已打印" : "未打印",
ElectronicReport = a.ElectronicReport,
Organization = b.Name,
Customer = c.Name,
SupplierName = d.Name
};

可以看出连接了三张表去查询也就是 join....on.... ,然后加上排序,这样代码会偏多一点。

那么我们来看看使用lambda表达式会怎么样:

 var query = db.ExaminationOrderInfo.Where(e => e.IsDelete ==
&& (appointmentResult == null || e.AppointmentResult == appointmentResult)
&& (isPaperReport == null || e.IsPaperReport == isPaperReport)
&& (examinationStatus == null || e.ExaminationStatus == examinationStatus)
&& (string.IsNullOrEmpty(name) || e.Name == name)
&& (string.IsNullOrEmpty(customer) || e.Customerinfo.Name == customer)
&& (string.IsNullOrEmpty(id) || e.IdCard == id)
&& (string.IsNullOrEmpty(organizationName) || e.ExaminationOrganization.Name == organizationName)
&& (string.IsNullOrEmpty(supplierName) || e.ExaminationOrganization.SupplierInfo.Name == supplierName)).Select(e => new ExaminationOrderInfoDto
{
Type = e.Type == ? "取消预约" : "预约",
IdCard = e.IdCard,
Name = e.Name,
Expire = DateTime.Now > e.ExaminationTime ? "是" : "否",
StartTime = e.StartTime.ToString(),
EndTime = e.EndTime.ToString(),
AppayTime = e.AppayTime,
ExaminationTime = e.ExaminationTime.ToString(),
AppointmentResult = e.AppointmentResult == ? "成功" : e.AppointmentResult == ? "失败" : "待处理",
ExaminationStatus = e.ExaminationStatus == ? "已登记" : "未登记",
IsPaperReport = e.IsPaperReport == ? "已打印" : "未打印",
ElectronicReport = e.ElectronicReport,
Organization = e.ExaminationOrganization.Name,
Customer = e.Customerinfo.Name,
SupplierName = e.ExaminationOrganization.SupplierInfo.Name
}).OrderBy(e => e.AppayTime);

我们可以看到使用lambda表达式就没有太多的连接代码了,如果想要查询的话,直接就e.Customerinfo.Name 可以得到Customerinfo表的name了,这是为什么呢,因为ExaminationOrderInfo这个实体中有这个字段:

 public Customerinfo Customerinfo { get; set; }

这就将ExaminationOrderInfo和Customerinfo连接起来了,一个ExaminationOrderInfo对应一个Customerinfo,而相信大家也留意到了ExaminationOrganization这个表了,里面对应的关系是这样的:

public ICollection<ExaminationOrderInfo> ExaminationOrderInfo { get; set; }

也就是说一个ExaminationOrganization对应多个ExaminationOrderInfo。那么还有 e.ExaminationOrganization.SupplierInfo.Name,为何需要这样呢,是因为ExaminationInfo是通过ExaminationOrganization这张表和Supplier连接的。

相信大家已经清楚了这两张查询方式的不同,建议大家两种都去了解和熟悉,这样遇到问题的时候有多种思考方式,不过我一般建议大家用lambda表达式,这样代码非常简洁,易读性也较高。

最后,有什么错误请指出,互相学习。谢谢大家!