2016-08-08 3 views
0

Soooo ...私はタスクをパラメータに基づいてサーバー上でxmlsを収集するツールを書いています。ここでlinqどこに交差が含まれています

は、私が持っているコードは、これまでのところです:

private List<string> GetListXmls(string strPath, List<string> colctNames) 
{ 
    string regexExpr = "\\\\\\\\" + strStager + "\\\\APPLI\\\\" + strEnvir + "\\\\[a-zA-Z]{3}\\\\Bin\\\\"; 

    if(colctNames == null) 
    { 
      List<string> xmlColct = Directory.GetFiles(strPath, "*.*", SearchOption.AllDirectories) 
                .Where(x => Regex.IsMatch(x, regexExpr, RegexOptions.IgnoreCase) && 
                   x.ToLower().Contains("msgtrait") && 
                   x.EndsWith(".xml")) 
                .ToList(); 

      return xmlColct; 
    } 
    else 
    { 
     List<string> xmlColct = Directory.GetFiles(strPath, "*.*", SearchOption.AllDirectories) 
                .Where(x => Regex.IsMatch(x, regexExpr, RegexOptions.IgnoreCase) && 
                   x.ToLower().Contains("msgtrait") && 
                   x.EndsWith(".xml")) 
                .ToList(); 

     List<string> finalList = new List<string>(); 
     foreach (string strFich in xmlColct) 
     { 
      if (colctNames.Any(item => strFich.ToLower().Contains(item.ToLower()))) 
      { 
       finalList.Add(strFich); 
      } 
     } 

     return finalList; 
     // include some kind of linq method to get only what I want instead of stripping down the list... 
    } 


} 

基本的に必要がABN_msgTrait.xmlに一致するサーバー上の任意のファイルを取得することです。ユーザーがORLUQMまたはBLABLABLAのみを検索している場合は、すべての結果を必要なものを削除するのではなく、にが必要です。リストxmlColctは、ORLという名前を含む可能性のあるパスのリストです(ORL_msgtrait.xml)。

私の質問は:foreachを私がlinq要求で行っている方法をマージするを回避するは、すべてのxmlsを取得し、不要なものを取り除かなければならないのですか?

+1

質問は不明ですか?あなたは実際にそこにいないことを実際に望んでいますか? –

+0

どうしたのですか? – itsme86

+0

Ooh!ごめんなさい!私の質問を編集します。 – hsim

答えて

1
List<string> xmlColct = Directory.GetFiles(strPath, "*.*", SearchOption.AllDirectories) 
    .Where(x => Regex.IsMatch(x, regexExpr, RegexOptions.IgnoreCase) && 
     x.ToLower().Contains("msgtrait") && 
     x.EndsWith(".xml") && 
     colctNames.Any(item => x.ToLower().Contains(item.ToLower()))) 
    .ToList(); 

しかし、あなたはすでに正規表現を使用している場合は、それに追加することができる。

  1. それは名前エリアで「.xmlファイル」
  2. が含まれ、 『msgtrait』で終わる
  3. Anyの一部にはstring.Joinの任意の値のパターンを形成するためにcolctNames
関連する問題