0

I have an xml file and want to remove any row from it. For this reason, I assign each row of the xml file to a list:

List<string[]> lines = new List<string[]>();
List<string> clmns = new List<string>();//each row is written to clmns
...
lines.Add(clmns.ToArray());

As you may expect, lines list will be like

{ "1", "2", ... },
{ "4", "5", ... },
...

Assume that I want to delete the row (the array in lines) holding "5". How can I make it?

I thought I can delete the array by using lines.RemoveAt(1). But, I couldn't find a way to find the index of array holding the search string.

3 Answers 3

5

You are awfully close. An array is just an object, so the following will work:

lines.Remove(lines.First(a => a.Contains("5")));

This gets the first array in the collection with a "5" and returns the array object to Remove which then removes it.

To remove all of them:

lines.RemoveAll(a => a.Contains("5"));
Sign up to request clarification or add additional context in comments.

6 Comments

That's great! It works. Thank you! Should I study LINQ to learn such statements?
@Siha: FYI: lines.First will throw an exception if no line with a "5" is found. If this is not desired, use lines.FirstOrDefault instead and check its result before passing it to lines.Remove
A valuable comment. Thank you.
Thanks for the FirstOrDefault comment, that can bite you pretty hard :). As far as learning LINQ, yes and no. I used a LINQ extension method, and passed it a lambda expression. Learning about those will take you a different route than learning about LINQ itself (since it is basically a language unto itself, similar to SQL). In the end, learning both will be of great benefit though!
@Siha, as an aside (if you do start studying LINQ) the example I gave uses LINQ-To-Objects. The syntax is the same for all versions of LINQ, but they are separate things. For general use, the extensions are enough :)
|
3

List.FindIndex method can help you here.

int index = lines.FindIndex(x=> x.Contains("5"));
if(index >= 0)
{
   lines.RemoveAt(index);
}

1 Comment

Thank you. This is also a useful one!
1

You need to loop to the array list and remove it manually. Assuming you just want to delete the first "5".

foreach(var line in lines)
{
 if(line.Contains("5"))
 {
  lines.Remove(line);
  break;
 }
}

4 Comments

This will throw an exception because you're modifying the foreach enumerable within the loop.
@itsme86 No it won't. It will if I iterate one more time.
Interestingly enough, it won't break if you iterate backwards. Besides that, I'm not sure what this gains you over using .First, .FirstOrDefault, or something like that.
Showing how to do it without using LINQ. Especially for people who are new to C# who hasn't tackled LINQ.

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.