C#如何比较字符串?
有两个日志行表格,“当前员工”和“项目”。它们共享同一字段“Employee ID”,该字段具有相同的下拉菜单和相同的值。下拉值包含所有员工的ID,包括以前的员工。只有“当前员工”日志行表单中使用的ID才属于当前员工。我们需要确保所有项目仅分配给当前员工。如何进行这种比较?以下是我所拥有的,但出于某种原因,它什么也做不了:C#如何比较字符串?,c#,asp.net,.net,C#,Asp.net,.net,有两个日志行表格,“当前员工”和“项目”。它们共享同一字段“Employee ID”,该字段具有相同的下拉菜单和相同的值。下拉值包含所有员工的ID,包括以前的员工。只有“当前员工”日志行表单中使用的ID才属于当前员工。我们需要确保所有项目仅分配给当前员工。如何进行这种比较?以下是我所拥有的,但出于某种原因,它什么也做不了: string[] employeeIDs1 = {"EMP-01", "EMP-02", "EMP-03", "EMP-04", "EMP-05"}; // all emp
string[] employeeIDs1 = {"EMP-01", "EMP-02", "EMP-03", "EMP-04", "EMP-05"}; // all employee ID's that exist in the drop down of "Current Employees"
string[] employeeIDs2 = {"EMP-01", "EMP-02", "EMP-03", "EMP-04", "EMP-05"}; // all employee ID's that exist in the drop down of "Projects"
bool AllFound = true;
int Entries = 0;
bool errorMessage = false;
for (int i = 0; i < Entries; i++) {
string[] idEntered1 = new string[Entry.Count];
string[] idEntered2 = new string[Entry.Count];
foreach (string s in employeeIDs2) {
if (String.Equals(s, idEntered1[i], StringComparison.OrdinalIgnoreCase)) {
AllFound = false;
break;
}
}
}
if (!AllFound) {
errorMessage = true;
}
else
{
errorMessage = false;
}
string[]employeeIDs1={“EMP-01”、“EMP-02”、“EMP-03”、“EMP-04”、“EMP-05”};//“当前员工”下拉列表中存在的所有员工ID
字符串[]employeeIDs2={“EMP-01”、“EMP-02”、“EMP-03”、“EMP-04”、“EMP-05”};//“项目”下拉列表中存在的所有员工ID
bool AllFound=true;
整数项=0;
bool errorMessage=false;
for(int i=0;i
对不起,我不确定我是否正确理解了您的问题,您可以使用LINQ方法查看以下列表中没有多少员工
[TestMethod]
public void TestNoOldEmployees_In_List()
{
string[] employeeIDs1 = { "EMP-01", "EMP-02", "EMP-03", "EMP-04", "EMP-05" };
string[] employeeIDs2 = { "EMP-01", "EMP-02", "EMP-03", "EMP-04", "EMP-05"};
var oldEmployeesCount = employeeIDs1.Where(x => !employeeIDs2.Contains(x)).Count();
Assert.AreEqual(0, oldEmployeesCount);
}
我不明白你想做什么,但你永远不会进入那个for…循环。条目=0;i=0<代码>条目等于零,因此它不会进入循环尝试以下内容:字符串[]notCurrentEmployees=employeeIDs2。其中(x=>!employeeIDs1.Contains(x)).ToArray()@Steve假设您在“当前员工”中输入了2名员工,在下拉列表中可用的5名员工中只有Emp-01和Emp-02。这意味着“项目”表格中的所有项目必须为所有项目分配Emp-01或Emp-02员工ID。如果用户无意中输入Emp-03,则会发出一条错误消息。我想告诉您的是:根据发布的代码,I<条目总是错误的。您从未进入循环,Allfound始终为true,errormessage设置为false。我建议您使用调试器并检查代码流。假设您在“当前员工”中输入了2名员工,在下拉列表中可用的5名员工中只有Emp-01和Emp-02。这意味着“项目”表格中的所有项目必须为所有项目分配Emp-01或Emp-02员工ID。如果用户无意中输入Emp-03,则应发出错误消息。