C# 需要从C中的ArrayList中删除值

C# 需要从C中的ArrayList中删除值,c#,arraylist,C#,Arraylist,所以我被要求删除以字母G、GE、G=或Z结尾的巡更码。唯一的缺点是我相信我们在很多页面上都使用了这个调用,这就是我不能首先更改数据库调用的原因,所以我想专门为这个人做这件事。我的代码正在调用一个arrayList,它填充了我们所有的代码。我有没有办法把上面写着字母的旅行团删除。这就是我要做的 public void LoadTourCodes() { ddlTourCode.Items.Clear(); if (ddlTourCode.Visible) {

所以我被要求删除以字母G、GE、G=或Z结尾的巡更码。唯一的缺点是我相信我们在很多页面上都使用了这个调用,这就是我不能首先更改数据库调用的原因,所以我想专门为这个人做这件事。我的代码正在调用一个arrayList,它填充了我们所有的代码。我有没有办法把上面写着字母的旅行团删除。这就是我要做的

public void LoadTourCodes()
{
    ddlTourCode.Items.Clear();

    if (ddlTourCode.Visible)
    {
        ddlTourCode.Items.Add(new ListItem(" ", ""));

        ArrayList tourCodes;
        tourCodes = EblTipTours.FindTourCodes();

        foreach (string tourCode in tourCodes)
        {                   
            ddlTourCode.Items.Add(tourCode);
        }
    }
}

您可以使用LINQ执行此操作,如下所示:

var toRemove = new []{"G", "GE", "G=", "Z"};
foreach (string tourCode in tourCodes.Where(code => !toRemove.Any(suffix => code.EndsWith(suffix)))) {
    ddlTourCode.Items.Add(tourCode);
}
string[] toRemove = new string[] {"G", "GE", "G=", "Z"};
foreach (string tourCode in tourCodes) {
    bool good = true;
    foreach (string suffix in toRemove) {
        if (tourCode.EndsWith(suffix)) {
            good = false;
            break;
        }
    }
    if (!good) continue;
    ddlTourCode.Items.Add(tourCode);
}
如果您不能使用LINQ,因为它是一个遗留系统,您可以像这样重写代码:

var toRemove = new []{"G", "GE", "G=", "Z"};
foreach (string tourCode in tourCodes.Where(code => !toRemove.Any(suffix => code.EndsWith(suffix)))) {
    ddlTourCode.Items.Add(tourCode);
}
string[] toRemove = new string[] {"G", "GE", "G=", "Z"};
foreach (string tourCode in tourCodes) {
    bool good = true;
    foreach (string suffix in toRemove) {
        if (tourCode.EndsWith(suffix)) {
            good = false;
            break;
        }
    }
    if (!good) continue;
    ddlTourCode.Items.Add(tourCode);
}

您可以使用LINQ执行此操作,如下所示:

var toRemove = new []{"G", "GE", "G=", "Z"};
foreach (string tourCode in tourCodes.Where(code => !toRemove.Any(suffix => code.EndsWith(suffix)))) {
    ddlTourCode.Items.Add(tourCode);
}
string[] toRemove = new string[] {"G", "GE", "G=", "Z"};
foreach (string tourCode in tourCodes) {
    bool good = true;
    foreach (string suffix in toRemove) {
        if (tourCode.EndsWith(suffix)) {
            good = false;
            break;
        }
    }
    if (!good) continue;
    ddlTourCode.Items.Add(tourCode);
}
如果您不能使用LINQ,因为它是一个遗留系统,您可以像这样重写代码:

var toRemove = new []{"G", "GE", "G=", "Z"};
foreach (string tourCode in tourCodes.Where(code => !toRemove.Any(suffix => code.EndsWith(suffix)))) {
    ddlTourCode.Items.Add(tourCode);
}
string[] toRemove = new string[] {"G", "GE", "G=", "Z"};
foreach (string tourCode in tourCodes) {
    bool good = true;
    foreach (string suffix in toRemove) {
        if (tourCode.EndsWith(suffix)) {
            good = false;
            break;
        }
    }
    if (!good) continue;
    ddlTourCode.Items.Add(tourCode);
}

是的,我们没有使用LINQ,看起来第二部分工作得非常完美!!非常感谢您的帮助,我理解您对这些标志所做的操作,只需删除它们,而不用添加它们。完美的是的,我们没有使用LINQ,看起来第二部分工作得非常完美!!非常感谢您的帮助,我理解您对这些标志所做的操作,只需删除它们,而不用添加它们。完美的