0

I tried to get a special count where I want to remove results where the userKeysActionsArray is a string that contains integer like this way (JSON array) : [88254,8547,8569]

But when I tried to remove with a contains method and the NOT operator, I couldn't get the right number I expected.

I don't know how to resolve this problem.

Thanks a lot for your help.

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;
6
  • I believe Contains is a method that requires an input parameter, so try changing your second last line from q.userKeysActionsArray.Contains to q.userKeysActionsArray.Contains(input value) Commented Jan 7, 2021 at 9:35
  • 1
    You are missing affectation to the count variable, is that a typo ? Commented Jan 7, 2021 at 9:36
  • @ArwynFr yes sorry typo error :-) will update the post. Thanks Commented Jan 7, 2021 at 9:42
  • @Scopperloit I don't know how to change it to test the 4 possibilities.Like this ? query.Where(q => !((q.userKeysActionsArray.Contains(chance1) && q.userKeysActionsArray.Contains(chance2) && q.userKeysActionsArray.Contains(chance3) && q.userKeysActionsArray.Contains(chance4)))); Commented Jan 7, 2021 at 9:48
  • Is each chance an array or a single string? Commented Jan 7, 2021 at 9:48

2 Answers 2

2

You can accumulate where conditions on the query in a foreach:

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();
Sign up to request clarification or add additional context in comments.

3 Comments

I will try this, could it be possible that the fact that the string "userKeysActionsArray" could be null that it will cause troubles?
This is what I would have done as well, foreach is your friend here. If you're worried about NullReferenceExceptions, always check for null first in boolean expressions. They always evaluate from left to right. Try query.Where(a => a.userKeysActionsArray != null && !a.userKeysActionsArray.Contains(value));
I've added a null-conditional operator and a null-coalescing operator to handle the null case
0

You can do it as below -

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.