简体   繁体   English

替换字典C#中的单引号

[英]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 我也想处理这个单引号,因为当它包含单引号时,我的下面的JS函数没有命中

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. 使用.Select不会修改原始集合。 It projects them into a new IEnumerable which you can use however you wish. 它将它们投影到一个新的IEnumerable中,您可以根据需要使用它。 Right now you're just throwing the result away. 现在,您只是放弃结果。 You can instead use them to create a new Dictionary: 您可以改用它们来创建新的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. 或者,如果您可以使用迭代方法而不是功能方法,则可以使用ForEach遍历字典并覆盖旧值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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