Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#字符串不包含_C#_Asp.net Mvc_Contains - Fatal编程技术网

C#字符串不包含

C#字符串不包含,c#,asp.net-mvc,contains,C#,Asp.net Mvc,Contains,我正在尝试从“奖学金”列表中删除在其“Student_state”属性中不包含字符串“state”的项目 if (States != "") { scholarships.RemoveAll(s => !s.Student_state.Contains(States)); scholarships.RemoveAll(s => s.Student_state == null);

我正在尝试从“奖学金”列表中删除在其“Student_state”属性中不包含字符串“state”的项目

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

这个!性格并没有做到这一点。有什么想法吗?

您测试的是变量状态,而不是字符串“States”(当然,除非该变量是“States”)

您的代码应该是:

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

你能试试这个吗?语句的顺序在这里很重要

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

是否可能存在案件敏感性问题?Contains执行区分大小写的测试。在过去,我使用了以下方法来实现不区分大小写的测试:

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

只选你想要的怎么样

if (States != "")
{
    scholarships = scholarships.Where(s=> !string.IsNullOrEmpty(s.Student_state) && s.Student_state.Contains(States)).ToList();
}

请用s.Student_state进行详细说明。Student_state是实体奖学金的一个属性。's'是lambda语句的一部分's=>s'学生州'是状态还是位置?根据字符串是否包含值删除项可能会产生不良副作用。例如,如果将其用于“弗吉尼亚州”等地州,“西弗吉尼亚州”学生的奖学金也将被取消。很好。这是一个位置。student_state是一个自定义位置对象、字符串列表/字典或其他东西?对不起,应该更清楚一点,它是一个变量“States”。@SRiley22,好的,你能发布一些示例数据吗?如果是这种情况,那么代码应该可以工作。如果你看函数的返回值,它是0吗?更一般地说,你怎么知道它没有像你期望的那样工作呢?这就是问题所在。谢谢