1

我有一个返回<string,string>键值对列表的 linq 语句。问题是键中的所有值都需要替换。有没有办法在 linq 的选择中进行替换而不必遍历整个列表?

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

1 回答 1

6

您不能更改密钥,但可以使用新密钥返回一个新对象:

 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 
                           };

或者如果它必须是 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);
于 2012-05-17T20:55:49.243 回答