在来自 Entity Framework 的查询中检查字符串是否不包含 C# 中字符串数组的字符串

Check if string does not contain strings of an array of string in C# in query from Entity Framework

我试图获得一个特殊的计数,我想删除结果,其中 userKeysActionsArray 是一个包含整数的字符串,就像这样(JSON 数组):[88254,85​​47,8569]

但是当我尝试使用 contains 方法和 NOT 运算符删除时,我无法获得我期望的正确数字。

我不知道如何解决这个问题。

非常感谢您的帮助。

int count;

var query = (from a in _db.ApplicationSet
             from h in a.ApplicationApprobationStatusHistorySet
             from u in a.Mission.MissionUserSet
             where a.Mission.isDeleted == false
                && a.Mission.isArchived == false
                && a.isRefused == false
                && a.ApplicationApprobationStatus.shortCode == "APPLICATION_APPROBATION_STATUS_LM_WAITING"
                && u.userKey == ContextUser.id
                && h.ApplicationApprobationStatus.shortCode == "APPLICATION_APPROBATION_STATUS_LM_WAITING" && h.isDeleted == false
              orderby a.applicationDate
              select new ApplicationCandidateListing
              {
                  applicationKey = a.id,
                  missionKey = a.Mission.id,
                  candidateKey = a.Candidate.id,
                  statusKey = a.ApplicationStatus.id,
                  clientKey = a.Mission.Client.id,
                  candidateFirstName = a.Candidate.firstName,
                  candidateLastName = a.Candidate.lastName,
                  clientLabel = a.Mission.Client.name,
                  statusLabel = a.ApplicationStatus.label,
                  missionLabel = a.Mission.label,
                  applicationDate = a.applicationDate,
                  isCVreserveList = a.isCVreserveList,
                  isCVviewed = a.isCVviewed,
                  refuseDate = a.refuseDate,
                  isShortListed = a.isShortListed,
                  candidateGroupShortcode = a.Candidate.CandidateGroup.shortCode,
                  approbationStatusIcons = a.ApplicationApprobationStatus.iconCSS,
                  approbationStatusShortCode = a.ApplicationApprobationStatus.shortCode,
                  isInternal = a.Candidate.isInternal,
                  lineManagerKey = ContextUser.id,
                  userKeysActionsArray = h.userKeysActionsArray,
                  commentCount = a.ApplicationCommentSet.Where(ac => ac.isPrivateLineManager != true).Count()
              }).Distinct();

string chance1 = "[" + ContextUser.id.ToString() + "]";
string chance2 = "[" + ContextUser.id.ToString() + ",";
string chance3 = "," + ContextUser.id.ToString() + ",";
string chance4 = "," + ContextUser.id.ToString() + "]";

var values = new[] { chance1, chance2, chance3, chance4 };
query.Where(q => !(values.Any(q.userKeysActionsArray.Contains)));  

count = query.Count();

return count;

您可以在 foreach 的查询中累积 where 条件:

var query = // redacted

string chance1 = "[" + ContextUser.id.ToString() + "]";
string chance2 = "[" + ContextUser.id.ToString() + ",";
string chance3 = "," + ContextUser.id.ToString() + ",";
string chance4 = "," + ContextUser.id.ToString() + "]";

var values = new[] { chance1, chance2, chance3, chance4 };
foreach(var value in values)
{
    query = query.Where(a => !a.userKeysActionsArray?.Contains(value) ?? true);
}
return query.Count();

您可以按照下面的方式进行 -

query.Where(q => !(
(q.userKeysActionsArray.Contains(chance1) ||
q.userKeysActionsArray.Contains(chance2) || 
q.userKeysActionsArray.Contains(chance3) || 
q.userKeysActionsArray.Contains(chance4))));