我有一个linq语句,它返回<string,string>
个键值对的列表。问题是需要替换密钥中的所有值。有没有办法在选择linq时进行替换而不必迭代整个列表?
var pagesWithControl = from page in sitefinityPageDictionary
from control in cmsManager.GetPage(page.Value).Controls
where control.TypeName == controlType
select page; // replace "~" with "localhost"
答案 0 :(得分: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);