优化LINQ语句:提高查询效率和可读性
以下是优化后的LINQ语句: var q = ( from a in GetIQueryable() join b in Db.GetIQueryable<Equipment_Info>() on a.EquipmentId equals b.Id into tmp1 from bb in tmp1.DefaultIfEmpty() join c in Db.GetIQueryable<Equipment_Type>() on bb.EquipmentTypeId equals c.Id into tmp2 from cc in tmp2.DefaultIfEmpty() where a.Deleted == false && bb.Deleted == false && bb.IsEnable && cc.Deleted == false && cc.IsEnable && bb.EnterpriseId == _operator.EnterpriseId select new { a.Id, // 主键 a.RecordTime, // 记录时间 a.EquipmentId, // 设备Id bb.EquipmentName, // 设备名称 bb.EquipmentTypeId, // 设备类型Id cc.TypeName, // 设备类型名称 a.Remarks, // 备注 a.AlarmTime, // 报警时间 a.AlarmCode, // 报警编号 a.AlarmLevel, // 报警等级 a.AlarmDescription, // 报警描述 a.AlarmValue, // 报警值 a.AlarmCount, // 报警次数 a.AlamType, // 报警类型(0报警,1故障) AlamTypeName = a.AlamType == 0 ? '报警' : '故障', // 报警类型(0报警,1故障) a.SolveState, // 处理状态(0待处理,1已处理) SolveStateName = a.SolveState == 0 ? '待处理' : '已处理', // 处理状态(0待处理,1已处理) a.SolveUser, // 处理人员 a.SolveTime, // 处理时间 a.SolveResult, // 处理结果 } ) .Union //充电桩的 ( from a in GetIQueryable() join b in Db.GetIQueryable<Equipment_ChargingPileInfo>() on a.EquipmentId equals b.Id into tmp1 from bb in tmp1.DefaultIfEmpty() where a.Deleted == false && bb.Deleted == false && bb.IsEnable && bb.EnterpriseId == _operator.EnterpriseId select new { a.Id, // 主键 a.RecordTime, // 记录时间 a.EquipmentId, // 设备Id bb.EquipmentName, // 设备名称 EquipmentTypeId = "cdz", // 设备类型Id TypeName = EquipmentType.充电桩.ToString(), // 设备类型名称 a.Remarks, // 备注 a.AlarmTime, // 报警时间 a.AlarmCode, // 报警编号 a.AlarmLevel, // 报警等级 a.AlarmDescription, // 报警描述 a.AlarmValue, // 报警值 a.AlarmCount, // 报警次数 a.AlamType, // 报警类型(0报警,1故障) AlamTypeName = a.AlamType == 0 ? '报警' : '故障', // 报警类型(0报警,1故障) a.SolveState, // 处理状态(0待处理,1已处理) SolveStateName = a.SolveState == 0 ? '待处理' : '已处理', // 处理状态(0待处理,1已处理) a.SolveUser, // 处理人员 a.SolveTime, // 处理时间 a.SolveResult, // 处理结果 } ) .Union //充电枪的 ( from a in GetIQueryable() join b in Db.GetIQueryable<Equipment_ChargingGunsInfo>() on a.EquipmentId equals b.Id into tmp1 from bb in tmp1.DefaultIfEmpty() join c in Db.GetIQueryable<Equipment_ChargingPileInfo>() on bb.ChargingPileId equals c.Id into tmp2 from cc in tmp2.DefaultIfEmpty() where a.Deleted == false && bb.Deleted == false && bb.IsEnable && cc.Deleted == false && cc.IsEnable && cc.EnterpriseId == _operator.EnterpriseId select new { a.Id, // 主键 a.RecordTime, // 记录时间 a.EquipmentId, // 设备Id bb.EquipmentName, // 设备名称 EquipmentTypeId = "cdq", // 设备类型Id TypeName = EquipmentType.充电枪.ToString(), // 设备类型名称 a.Remarks, // 备注 a.AlarmTime, // 报警时间 a.AlarmCode, // 报警编号 a.AlarmLevel, // 报警等级 a.AlarmDescription, // 报警描述 a.AlarmValue, // 报警值 a.AlarmCount, // 报警次数 a.AlamType, // 报警类型(0报警,1故障) AlamTypeName = a.AlamType == 0 ? '报警' : '故障', // 报警类型(0报警,1故障) a.SolveState, // 处理状态(0待处理,1已处理) SolveStateName = a.SolveState == 0 ? '待处理' : '已处理', // 处理状态(0待处理,1已处理) a.SolveUser, // 处理人员 a.SolveTime, // 处理时间 a.SolveResult, // 处理结果 } );
原文地址: https://www.cveoy.top/t/topic/p0mX 著作权归作者所有。请勿转载和采集!