简体   繁体   中英

Replace single quote from Dictionary C#

In my below code I want to replace single quote with some other say "&" but it's not reflecting.

if(this.selectdFilterValues.Any(kvp => kvp.Value.Contains("'")))
{
    this.selectdFilterValues.Select(kvp => kvp.Value.Replace("'", "&"));
}  

Also I want to handle this single quote as when it contains single quote my below JS function does not hit

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", 
    "<script type='text/javascript'>initViz('" + 
    JsonConvert.SerializeObject(this.selectdFilterValues) + 
    "');</script>", false);

Using .Select does not modify the original collection in place. It projects them into a new IEnumerable which you can use however you wish. Right now you're just throwing the result away. You can instead use them to create a new Dictionary:

this.selectedFilterValues = this.selectdFilterValues.ToDictionary(x => x.Key, x => x.Value.Replace("'", "&"));

Or, if you're okay with using an iterative approach instead of a functional one you can simply loop through the Dictionary using a ForEach and overwriting the old values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM