Linq2SQL检查item是否为null

时间:2009-05-20 08:22:45

标签: c# linq-to-sql

foreach(在新DataAccess.IncidentRepository()中的var事件.GetItems()。其中​​(                         i => (startDate == null || i.IncidentDate> = startDate)                         &安培;&安培; (endDate == null || i.IncidentDate< = endDate)                         &安培;&安培; (shiftId == null || i.ShiftId == shiftId)                         &安培;&安培; (processAreaId == null || i.ProcessAreaId == processAreaId)                         &安培;&安培; (plantId == null || i.PlantId == plantId)))

如果i.PlantId == plantIdplantId,我可以null无法添加吗?

由于

2 个答案:

答案 0 :(得分:2)

var incident in new DataAccess.IncidentRepository().GetItems().Where(
                    i => i.IncidentDate >= startDate 
                    && i.IncidentDate <= endDate 
                    && i.ShiftId == shiftId 
                    && i.ProcessAreaId == processAreaId
                    && (plantId == null || i.PlantId == plantId)))

或者,您可以:

var incidents = new DataAccess.IncidentRepository().GetItems().Where(
                    i => i.IncidentDate >= startDate 
                    && i.IncidentDate <= endDate 
                    && i.ShiftId == shiftId 
                    && i.ProcessAreaId == processAreaId));

if (plantId != null)
    incidents = incidents.Where(i => i.PlantId == plantId);

foreach (var incident in incidents) {
   // ...
}

答案 1 :(得分:0)

var incident in new DataAccess.IncidentRepository().GetItems().Where(
                    i => i.IncidentDate >= startDate 
                    && i.IncidentDate <= endDate 
                    && i.ShiftId == shiftId 
                    && i.ProcessAreaId == processAreaId
                    && object.Equals(i.PlantId, plantId)))