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?
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)
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.