简体   繁体   中英

update value of Key in key value pair

I have a linq statement that returns a list of <string,string> key value pairs. The problem is that all the values in the key need to be replaced. Is there a way to do a replace in the select of the linq without having to iterate through the entire list?

var pagesWithControl = from page in sitefinityPageDictionary
                       from control in cmsManager.GetPage(page.Value).Controls
                       where control.TypeName == controlType
                       select page; // replace "~" with "localhost"

You can't change the key, but you can return a new object with the new key:

 var pagesWithControl = from page in sitefinityPageDictionary
                   from control in cmsManager.GetPage(page.Value).Controls
                   where control.TypeName == controlType
                   select new 
                           { 
                             Key = page.Key.Replace("~",localhost"), 
                             page.Value 
                           };

or if it has to be a KeyValuePair:

var pagesWithControl =  
   from page in sitefinityPageDictionary
   from control in cmsManager.GetPage(page.Value).Controls
   where control.TypeName == controlType
   select 
   new KeyValuePair<TKey,TValue>(page.Key.Replace("~",localhost"), page.Value);

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