[英]Linq to Generic list C# Data Filtering issue
I am having a problem with following linq query in my c# code. 我在C#代码中执行linq查询时遇到问题。 I want to exclude data that has jobChangeTypeId
Unallocate
or Delete
but its bringing them in the resultset. 我想排除具有jobChangeTypeId取消
Unallocate
或Delete
但将它们带入结果集中的数据。
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);
}
}
}
It's an AND not an OR if you want to exclude both of them 如果您想同时排除两者,则它是AND而不是OR
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);
}
}
}
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.