3355307
3355307

Reputation: 1678

Linq to Generic list C# Data Filtering issue

I am having a problem with following linq query in my c# code. I want to exclude data that has jobChangeTypeId Unallocate or Delete but its bringing them in the resultset.

foreach (EmployeejobAudit empjobAudit in list)
{
     int iEmployeeServiceId = empjobAudit.EmployeeServiceId;

     PushNotificationData.jobAuditRow[] jobAuditList = empjobAudit.jobAuditList;

     var jobCallQuery = (from job in jobAuditList
                         where ((from dc in dataset.jobCall select dc.jobId).Contains(job.jobId)) && 
                        ( job.jobChangeTypeId != (int) Common.jobChangeTypeId.Unallocate  || job.jobChangeTypeId != (int) Common.jobChangeTypeId.Delete)                                   
                         select job).Distinct();

     if (jobCallQuery.Any())
     {
         foreach (var item in jobCallQuery)
         {
             System.Diagnostics.Debug.WriteLine("jobId {0}  Employee ServiceID {1} jobChange Type ID {2}", item.jobId, item.EmployeeServiceId, item.jobChangeTypeId);
         }
     }
}

Upvotes: 0

Views: 99

Answers (2)

Omri Aharon
Omri Aharon

Reputation: 17064

You're doing || instead of &&. The problem is that when using || one of the two expressions will always be true, thus be included.

This:

job.jobChangeTypeId != (int) Common.jobChangeTypeId.Unallocate  
|| job.jobChangeTypeId != (int) Common.jobChangeTypeId.Delete

Needs to be changed to this:

job.jobChangeTypeId != (int) Common.jobChangeTypeId.Unallocate  
&& job.jobChangeTypeId != (int) Common.jobChangeTypeId.Delete

Upvotes: 1

Benji_9989
Benji_9989

Reputation: 173

It's an AND not an OR if you want to exclude both of them

foreach (EmployeejobAudit empjobAudit in list)
{
     int iEmployeeServiceId = empjobAudit.EmployeeServiceId;

     PushNotificationData.jobAuditRow[] jobAuditList = empjobAudit.jobAuditList;

     var jobCallQuery = (from job in jobAuditList
                         where ((from dc in dataset.jobCall select dc.jobId).Contains(job.jobId)) && 
                        ( job.jobChangeTypeId != (int) Common.jobChangeTypeId.Unallocate && job.jobChangeTypeId != (int) Common.jobChangeTypeId.Delete)                                   
                         select job).Distinct();

     if (jobCallQuery.Any()) //Useless because the following foreach will do it for you, but you can test if != null 
     {
         foreach (var item in jobCallQuery)
         {
             System.Diagnostics.Debug.WriteLine("jobId {0}  Employee ServiceID {1} jobChange Type ID {2}", item.jobId, item.EmployeeServiceId, item.jobChangeTypeId);
         }
     }
}

Upvotes: 1

Related Questions