6

I've got a list of strings called albumList the string has this structure: abc,abc,abc,abc,abc I ask the user to search an album to delete; if the album is found, it will be deleted from the list. There could be more than one album with the same name to be deleted from the list.

 private void searchAlbumName(string search)
        {
            string[] cellVal;
            foreach (string x in albumList)
            {
                cellVal = x.Split(',');
                if (cellVal[0].ToString().Equals(search))
                {
                    //do delete here
                }
            }
        }

I'm not sure how to remove every album with the search name

2 Answers 2

11

You can use RemoveAll to get the result:

albumList.RemoveAll(x => x.Split(',')[0].ToString().Equals(search))

For easier to read, you can use:

albumList.RemoveAll(x => x.Split(',').First().Equals(search))
Sign up to request clarification or add additional context in comments.

Comments

1

Using LINQ do

albumList.RemoveAll(x => cellVal[0].ToString().Equals(search)));

See this post.

Comments

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.