List of rows to delete

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 . After adding, I want to remove any entries from this list containing the text "FID" or "EXCLUDE" .

I'm having trouble deleting a post, can someone help me?

+4
source share
6 answers

why add them when you want to remove them immediately 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(); 
+7
source

The following code example iterates through lineList and removes lines containing FID or EXCLUDE.

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

When deleting items, it is important to move the list in reverse order.

+3
source

You cannot delete items in your theFinalList list while you iterate over theFinalList in a foreach . In this case, you may receive a System.InvalidOperationException with the message "The collection has been changed, the enumeration operation may not be performed."

you need 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); } 
+1
source

to 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); } 
+1
source

Try the following:

 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(); 
+1
source

I think his 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 the regex for your needs (pay attention to the case, maybe multi-line, maybe compiled) and feel free to replace "\n" with Environment.NewLine

+1
source

All Articles