C# 更新键值对中键的值
我有一个linq语句,它返回键值对列表。问题是需要替换键中的所有值。有没有一种方法可以在linq的select中进行替换,而不必遍历整个列表C# 更新键值对中键的值,c#,linq,C#,Linq,我有一个linq语句,它返回键值对列表。问题是需要替换键中的所有值。有没有一种方法可以在linq的select中进行替换,而不必遍历整个列表 var pagesWithControl = from page in sitefinityPageDictionary from control in cmsManager.GetPage(page.Value).Controls where control.Ty
var pagesWithControl = from page in sitefinityPageDictionary
from control in cmsManager.GetPage(page.Value).Controls
where control.TypeName == controlType
select page; // replace "~" with "localhost"
不能更改键,但可以使用新键返回新对象:
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);
不能更改键,但可以使用新键返回新对象:
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);
现在假设您在方法签名上使用KeyValuePair和params关键字,并希望更改其中一个传递的KeyValuePair的值。继续:该更改将复制到方法调用方。现在假设您在方法签名上使用KeyValuePair和params关键字,并希望更改传递的KeyValuePair之一的值。继续:该更改将复制到方法调用方。