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 に答える