2009-04-14 4 views
0

私は、フィルタパスを表す文字列を持っています(例えば "Plant Filters \ Equipment \ Equipment List") リーフノードはFilter型で、残りはFilterFoldersです。これはLINQableですか?

私がする必要がある:各ノード(ない葉)スルー

  1. 行くと、それはのget - > [フォルダ
  2. はパスの次の項目をチェックし、それが
  3. 上からフォルダを一致するかどうか一致する場合は最後の1であり、次にフィルタが得られた場合
  4. 次に、フィルタをチェックして最後の文字列エントリと一致するかどうかを確認し、フォルダを取得して基本的に手順2に戻ります。

これを行うにはどうすればよいですか?それはコレクション、クラス、などの場合

+0

Linqではないコードサンプルはどうでしょうか?Linqまたはlambdaを使用できるかどうかを確認します。 –

答えて

0

まあ、私は、私の知る限りは何を知っているようlinqedすることができていないサンプルコード、 で少し混乱していたが... は、私は何を示唆して文字列配列を作成することですそのパスのsplit関数を使用して、その文字列を照会し、Switch演算子またはIf elseを使用します。

私は実際にサンプルを見る必要があるので、より正確に判断することができます。

これが役に立ちます。

1

これは再帰的な構造であり、LINQ標準クエリ演算子では処理できません。ここでは、再帰的ラムダ式を書くことを可能にするimplementation of the Y-combinatorを見つけることができます。あなたがそれを使うべきかどうかわからないのは、フードの下では、単純な問題に対するかなり複雑な解決策であるからです。これはimplementation of a recursive query operatorです(リンクされたブログエントリは正しい最終的な解決法ではないことに注意してください - それを正しくするために後の項目を読んでください)ははるかに簡単ですが、再帰ラムダ式を定義するという一般的な問題は解決しません。

0

ForEachIndex extnを定義した後、この方法を試しました。それをテストしていない(デュハ)、私は思うが動作する必要があります。

public static FilterBase FilterFromPath(this Site activeSite, string filterPath) 
    { 
     //"Catalog Filters\Default Filters\SP3D Report Filters\Types of Reports\Equipment\Equipment Material Take-Off"    
     Checks.IsNotNull(() => activeSite.ActivePlant); 

     FilterFolder currentFolder = null; 
     string plantFilters = CmnLocalizer.GetString(CmnResourceIDs.CmnFilterPlantFiltersFolder, "Plant Filters"); 
     string catalogFilters = CmnLocalizer.GetString(CmnResourceIDs.CmnFilterCatalogFiltersFolder, "Catalog Filters"); 

     if (filterPath.StartsWith(plantFilters)) 
      currentFolder = activeSite.ActivePlant.PlantModel.Folders[0] as FilterFolder; 
     else if (filterPath.StartsWith(catalogFilters)) 
      currentFolder = activeSite.ActivePlant.PlantCatalog.Folders[0] as FilterFolder; 
     else 
      throw new ArgumentException("Invalid filter root specified. Should start with Plant or Catalog Filters"); 

     IEnumerable<string> pathEntries = filterPath.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries).Skip(1); 
     Checks.IsNotNull(() => pathEntries); 
     if (pathEntries.Count() == 0) 
      throw new ArgumentException("Invalid filter path specified"); 

     int lastIndex = pathEntries.Count() - 1;    
     FilterBase filterFound = null; 

     pathEntries.ForEachIndex((item, index) => 
     { 
      if (index == lastIndex) 
      { 
       filterFound = currentFolder.ChildFilters.FirstOrDefault(f => f.Name.Equals(item)); 
       if (filterFound.Equals(default(FilterBase))) 
        throw new ArgumentException(string.Format("Filter '{0}' could not be found", item)); 
      } 
      else 
      { 
       currentFolder = currentFolder.ChildFolders.FirstOrDefault(f => f.Name.Equals(item)); 
       if (currentFolder.Equals(default(FilterFolder))) 
        throw new ArgumentException(string.Format("Folder '{0}' given in filter path not found", item)); 
      } 
     }); 

     return filterFound; 
    } 
関連する問題