C#如何比较字符串?

时间:2018-10-26 15:31:48

标签: c# asp.net .net

有两种日志行格式,“当前员工”和“项目”。他们共享相同的字段“ Employee ID”,具有相同的下拉菜单和相同的值。下拉值包含所有员工的ID,包括以前的员工。只有在“当前雇员”日志行格式中使用的那些ID才属于当前雇员。我们需要确保所有项目都仅分配给当前员工。如何进行这种比较?这是我所拥有的,但是由于某种原因它什么也没做:

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;
}

1 个答案:

答案 0 :(得分:0)

对不起,我不确定我是否正确理解了您的问题,您可以使用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);
}