2009-05-20 4 views
0

Еогеасп (вар инцидент в новом DataAccess.IncidentRepository(). GetItems(). Где ( я => (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)))Linq2SQL проверить, если элемент является нулевым

есть ли способ я могу i.PlantId == plantId не получать, если plantId is null?

Благодаря

ответ

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) { 
    // ... 
} 
+0

Это не имеет никакого смысла i.PlantId приходит для PlantID базы данных является параметр, переданный в метод. – 2009-05-20 08:25:59

+0

так что если plantId == null, то i.PlantId не следует добавлять в предложение where. – 2009-05-20 08:26:31

+0

Я не думаю, что это сработает. – 2009-05-20 08:28:57

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))) 
+0

Это не то, что хочет OP ... –

Смежные вопросы