theNoobGuy
theNoobGuy

Reputation: 1636

String list remove

I have this code:

List<string> lineList = new List<string>();

foreach (var line in theFinalList)
{
    if (line.PartDescription != "")
        lineList.Add(line.PartDescription + " " + line.PartNumber + "\n");
    else
        lineList.Add("N/A " + line.PartNumber + "\n");

    //
    //This is what I am trying to fix:
    if (lineList.Contains("FID") || lineList.Contains("EXCLUDE"))
        // REMOVE THE item in the lineList
}

I am trying to go through theFinalList in a foreach loop and add each line to a new list called lineList. Once added, I want to remove any entries from that list that contain the text "FID" or "EXCLUDE".

I am having trouble removing the entry, can someone help me?

Upvotes: 4

Views: 9534

Answers (6)

Tomas Jansson
Tomas Jansson

Reputation: 23472

Try this:

var excludingTexts = new [] { "FID", "EXCLUDE" }
lineList = lineList.Where(y => !excludingTexts.Any(x => line.PartDescription.Contains(x) || line.PartNumber.Contains(x))).ToList();

Or you can rewrite it as:

var excludingTexts = new [] { "FID", "EXCLUDE" }
List<string> lineList = (from line in theFinalList
                         where !excludingTexts.Any(x => line.PartDescription.Contains(x) || line.PartNumber.Contains(x))
                         select line.PartDescription != "" ? 
                                line.PartDescription + " " + line.PartNumber + "\n" : 
                                "N/A " + line.PartNumber + "\n"
                         ).ToList();

Upvotes: 1

Valentin Kuzub
Valentin Kuzub

Reputation: 12093

I think its more logical approach

Regex exclude = new Regex("FID|EXCLUDE");
foreach (var line in theFinalList.Where(
 ln => !exclude.Match(ln.PartDescription).Success && 
       !exclude.Match(ln.PartNumber ).Success))){

    string partDescription = "N/A";
    if(!string.IsNullOrWhiteSpace(line.PartDescription)){
        partDescription = line.PartDescription;
    }
    lineList.Add(partDescription  + " " + line.PartNumber + "\n");
}

edit regex for your needs (ignore case maybe or multiline, probably compiled too) and feel free to replace "\n" with Environment.NewLine

Upvotes: 1

Casperah
Casperah

Reputation: 4564

The following code sample iterates through the lineList and removes lines that contain FID or EXCLUDE.

for(int i = lineList.Count - 1; i >= 0; i--)
{
    if (lineList[i].Contains("FID") || lineList[i].Contains("EXCLUDE"))
       lineList.RemoveAt(i);
}

It is important to traverse a list in reverse order when deleting items.

Upvotes: 3

Yahia
Yahia

Reputation: 70369

try

foreach (var line in theFinalList)
{
    string T = "";
    if (line.PartDescription != "")
        T = line.PartDescription + " " + line.PartNumber + "\n";
    else
        T = "N/A " + line.PartNumber + "\n";

    if (!(T.Contains("FID") || T.Contains("EXCLUDE"))
        lineList.Add (T);
}

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 161012

why add them when you want to remove them right after:

lineList = theFinalList.Select( line => 
{
    if (line.PartDescription != "")
        return line.PartDescription + " " + line.PartNumber + "\n";
    else
        return "N/A " + line.PartNumber + "\n";
})
.Where(x => !(x.Contains("FID") || x.Contains("EXCLUDE")))
.ToList();

Upvotes: 7

user195488
user195488

Reputation:

You can't remove the items in your theFinalList list while you are iterating over theFinalList in a foreach loop. In this case, you may get System.InvalidOperationException with the message “Collection was modified; enumeration operation may not execute.”

you have to do something like this:

List<string> removals = new List<string>();

foreach (string s in theFinalList)
{
    //do stuff with (s);
    removals.Add(s);
}

foreach (string s in removals)
{
    theFinalList.Remove(s);
}

Upvotes: 1

Related Questions