1

So I have been asked to remove tour codes that end with the letter G, GE, G=, or Z. The only bad thing is I believe we use this call for a lot of pages and that is the reason I cant alter the database call in the first place so I want to do this specifically for this one person. My code is calling an arrayList that fills with all the tourcodes we have. Is there any way I can remove the tours with the letters above. Here is what I got to work with.

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

1 Answer 1

4

You can do it using LINQ, like this:

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

If you cannot use LINQ because it's a legacy system, you can rewrite the code like this:

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes we arent using LINQ and it looks as if the second part worked to perfection!! Thank you so much for the help and I understand what you are doing with the flags much easier then removing just not add it. Perfect!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.