优化linq to sql:var query1 = GetIQueryable;var query2 = DbGetIQueryableEquipment_Info;var query3 = DbGetIQueryableEquipment_Type;var q = from a in query1 join b in query2 on aEquipmentId equals
在优化这段代码时,可以考虑以下几点:
-
减少重复查询:可以将重复的查询提取出来,避免多次执行相同的查询语句。例如,可以将
query1和query2的结果缓存起来,避免多次执行GetIQueryable方法。 -
使用延迟加载:可以使用
AsQueryable方法将集合转换为延迟加载的IQueryable对象,避免立即加载数据。这样可以在需要时再进行数据加载,提高性能。 -
使用索引:如果查询中使用了
where子句,可以考虑在相关字段上创建索引,以提高查询效率。 -
避免重复计算:在查询的
select子句中,有些字段的值可以在查询的其他地方计算得到,可以将这些计算移动到查询的顶部,避免重复计算。 -
使用
SingleOrDefault替代DefaultIfEmpty:如果使用DefaultIfEmpty方法来处理左连接的情况,可以考虑使用SingleOrDefault方法来处理只有一个结果的情况,以减少不必要的操作。
综上所述,优化后的代码可以如下所示:
var query1 = GetIQueryable().AsQueryable(); var query2 = Db.GetIQueryable<Equipment_Info>().AsQueryable(); var query3 = Db.GetIQueryable<Equipment_Type>();
var q = (from a in query1 join b in query2 on a.EquipmentId equals b.Id into tmp1 from bb in tmp1.DefaultIfEmpty() where a.Deleted == false && bb.EnterpriseId == _operator.EnterpriseId select new { Id = a.Id, RecordTime = a.RecordTime, EquipmentId = a.EquipmentId, EquipmentName = bb.EquipmentName, EquipmentTypeId = bb.EquipmentTypeId, TypeName = query3.FirstOrDefault(c => c.Id == bb.EquipmentTypeId)?.TypeName, Remarks = a.Remarks, AlarmTime = a.AlarmTime, AlarmCode = a.AlarmCode, AlarmLevel = a.AlarmLevel, AlarmDescription = a.AlarmDescription, AlarmValue = a.AlarmValue, AlarmCount = a.AlarmCount, AlamType = a.AlamType, AlamTypeName = (a.AlamType == 0 ? "报警" : "故障"), a.SolveState, SolveStateName = (a.SolveState == 0 ? "待处理" : "已处理"), SolveUser = a.SolveUser, SolveTime = a.SolveTime, SolveResult = a.SolveResult, }) .Union ( from a in query1 join b in Db.GetIQueryable<Equipment_ChargingPileInfo>().AsQueryable() on a.EquipmentId equals b.Id into tmp1 from bb in tmp1.DefaultIfEmpty() where a.Deleted == false && bb.EnterpriseId == _operator.EnterpriseId select new { Id = a.Id, RecordTime = a.RecordTime, EquipmentId = a.EquipmentId, EquipmentName = bb.EquipmentName, EquipmentTypeId = "cdz", TypeName = EquipmentType.充电桩.ToString(), Remarks = a.Remarks, AlarmTime = a.AlarmTime, AlarmCode = a.AlarmCode, AlarmLevel = a.AlarmLevel, AlarmDescription = a.AlarmDescription, AlarmValue = a.AlarmValue, AlarmCount = a.AlarmCount, AlamType = a.AlamType, AlamTypeName = (a.AlamType == 0 ? "报警" : "故障"), a.SolveState, SolveStateName = (a.SolveState == 0 ? "待处理" : "已处理"), SolveUser = a.SolveUser, SolveTime = a.SolveTime, SolveResult = a.SolveResult, } ) .Union ( from a in query1 join b in Db.GetIQueryable<Equipment_ChargingGunsInfo>().AsQueryable() on a.EquipmentId equals b.Id into tmp1 from bb in tmp1.DefaultIfEmpty() join c in query2 on bb.ChargingPileId equals c.Id into tmp2 from cc in tmp2.DefaultIfEmpty() where a.Deleted == false && cc.EnterpriseId == _operator.EnterpriseId select new { Id = a.Id, RecordTime = a.RecordTime, EquipmentId = a.EquipmentId, EquipmentName = bb.EquipmentName, EquipmentTypeId = "cdq", TypeName = EquipmentType.充电枪.ToString(), Remarks = a.Remarks, AlarmTime = a.AlarmTime, AlarmCode = a.AlarmCode, AlarmLevel = a.AlarmLevel, AlarmDescription = a.AlarmDescription, AlarmValue = a.AlarmValue, AlarmCount = a.AlarmCount, AlamType = a.AlamType, AlamTypeName = (a.AlamType == 0 ? "报警" : "故障"), a.SolveState, SolveStateName = (a.SolveState == 0 ? "待处理" : "已处理"), SolveUser = a.SolveUser, SolveTime = a.SolveTime, SolveResult = a.SolveResult, } )
原文地址: https://www.cveoy.top/t/topic/hHZT 著作权归作者所有。请勿转载和采集!