2011-11-10 8 views
2

私は、いくつかのノードが設定されたTreeViewを持っています。問題は、このノードは異なるTagNameプロパティを持つことができますが、それらのいくつかは同じTextプロパティを持つことができます。TreeViewで同じTextプロパティを持つ "TreeNode"をクリーンアップする方法はありますか?

上記の各ノードから1つのノードしか持たないので、TreeViewは一意のノードをTextで持つことになります。

すべてのノードのリストを作成し、それらをフィルタリングしてから、新しいリストをTreeViewに追加しようとしています。ここに私のアプローチがあり、私はコンパイルしていない行についてコメントしました。

 //Remove Duplicated Nodes 
     List<TreeNode> oldPinGrpNodes = new List<TreeNode>(); 
     List<TreeNode> newPinGrpNodes = new List<TreeNode>(); 
     TreeNode tempNode; 


     foreach (TreeNode node in tvPinGroups.Nodes) 
     { 
      oldPinGrpNodes.Add(node); 
     } 

     foreach (TreeNode node in oldPinGrpNodes) 
     { 
      if (newPinGrpNodes.Contains(node.Text)) continue; //this does not compile! 
      //How to do a check in the IF statement above? 

      //Continue with adding the unique pins to the newList 
     } 

もしあなたが良いアイデアをお持ちでしたらお知らせください!

答えて

2

は、あなたの代わりにあなたの小切手を、以下のLINQクエリを試してみましたか?エラーを説明するための

if (newPinGrpNodes.Any(n => n.Text == node.Text)) continue; 
1

これを試してください。タイプTreeNodeのコレクションを、あなたがそこにstringを検索しよう - あなたのコンパイルエラーについては

//Remove Duplicated Nodes 
     List<TreeNode> oldPinGrpNodes = new List<TreeNode>(); 
     Dictionary<string, TreeNode> newPinGrpNodes = new Dictionary<string, TreeNode>(); 
     TreeNode tempNode; 


     foreach (TreeNode node in tvPinGroups.Nodes) 
     { 
     oldPinGrpNodes.Add(node); 
     } 

     foreach (TreeNode node in oldPinGrpNodes) 
     { 
     if (newPinGrpNodes.ContainsKey(node.Text)) continue; //this does not compile! 
     //How to do a check in the IF statement above? 

     //Continue with adding the unique pins to the newList 

     // do something like 
     newPinGrpNodes.Add(node.Text, node); 
     } 

newPinGrpNodes:私はSystem.LinqToLookup拡張メソッドを使用しています。

UPDATE:Dictionary<string, TreeNode>代わりのList<TreeNode>:パフォーマンスのために 、それはアイテムを検索するための辞書を使用することをお勧めします。

+0

感謝。しかし、コードはうまくいきませんでした(ちょうど古いリストと同じtreenodesを生成しました) –

+1

@ Sean87、Ahh、おそらく 'foreach'の' newPinGrpNodes'を変更しました –

+0

そう、私は 'oldPinGrpNoeds'だと思う:P –

1

if(newPinGrpNodes.Any(n => n.Text==node.Text)) continue;

1
foreach (TreeNode node in oldPinGrpNodes)  
{   
if (from m in newPinGrpNodesLookup where m.text=node.Text select m).first ==null) 
    continue; 
} 
関連する問題