Question

I am trying to delete items from a list 'scholarships' that do not contain string 'States' in its 'Student_state'attribute.

if (States != "")
            {
                scholarships.RemoveAll(s => !s.Student_state.Contains(States));
                scholarships.RemoveAll(s => s.Student_state == null);
            }

The ! character did not accomplish this. Any ideas?

Était-ce utile?

La solution

Is it possible a case sensitivity issue? String.Contains does a case sensitive test. I have used the following to achieve a case insensitive test in the past:

(s.Student_state.IndexOf(States, StringComparison.CurrentCultureIgnoreCase) == -1)

Autres conseils

You are testing for the variable States, not the string "States" (unless of course that variable is "States").

Your code should be:

if (States != "")
{
  scholarships.RemoveAll(s => !s.Student_state.Contains("States"));
  scholarships.RemoveAll(s => s.Student_state == null);
}

Could you try this? The sequence of statements is important here.

if (States != "")
{
    scholarships.RemoveAll(s => s.Student_state == null);
    scholarships.RemoveAll(s => !s.Student_state.Contains("States"));      
}

How about only select the ones you want?

if (States != "")
{
    scholarships = scholarships.Where(s=> !string.IsNullOrEmpty(s.Student_state) && s.Student_state.Contains(States)).ToList();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top