Replace string values in list

You have a few issues here: first, strings are immutable, so when you call .Replace you return a new string. Calling n.Replace doesn’t modify n. assigning to n in your anonymous function won’t affect the value that’s in your list. regardless of the above, you can’t change the content of your collection while enumerating it, … Read more

Convert List to Dictionary

Try the following var dictionary = sampleList .GroupBy(x => x.ResultString, x => x.ID) .ToDictionary(x => x.Key, x => x.ToList()); The GroupBy clause will group every Sample instance in the list by its ResultString member, but it will keep only the Id part of each sample. This means every element will be an IGrouping<string, int>. The … Read more

Better way to clean a string?

OK, consider the following test: public class CleanString { //by MSDN http://msdn.microsoft.com/en-us/library/844skk0h(v=vs.71).aspx public static string UseRegex(string strIn) { // Replace invalid characters with empty strings. return Regex.Replace(strIn, @”[^\w\.@-]”, “”); } // by Paolo Tedesco public static String UseStringBuilder(string strIn) { const string removeChars = ” ?&^$#@!()+-,:;<>’\’-_*”; // specify capacity of StringBuilder to avoid resizing StringBuilder … Read more