简体   繁体   English

C#如何比较字符串?

[英]C# How to compare strings?

There are two log line forms, "Current Employees" and "Projects".有两种日志行形式,“当前员工”和“项目”。 They share the same field "Employee ID", which has the same drop down menu with the same values.它们共享相同的字段“员工 ID”,该字段具有相同值的相同下拉菜单。 Drop down values contain IDs of all employees, including former employees.下拉值包含所有员工的 ID,包括前员工。 Only those IDs that were used in "Current Employees" log line form belong to current employees.只有在“当前员工”日志行表单中使用的 ID 才属于当前员工。 We need to make sure that all projects are assigned to current employees only.我们需要确保所有项目只分配给现有员工。 How to make this comparison happen?如何进行这种比较? Here is what I have but it doesn't do anything for some reason:这是我所拥有的,但由于某种原因它没有做任何事情:

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

Sorry, I am not sure I understood your problem correctly, You may use LINQ methods to see how many employees are not there in the list as below.抱歉,我不确定我是否正确理解了您的问题,您可以使用 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM