hsim
hsim

Reputation: 2080

linq where any intersect contains

Soooo... I am writing a tool which task is to collect xmls on servers based on parameters.

Here's the code I have so far:

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...
    }


}

Basically the need is to get any files on a server which match ABN_msgTrait.xml. My need is that if the user is seeking only ORL, UQM or BLABLABLA, the method will get only the needed list instead of stripping down all the results to what I need. Bear in mind that the list xmlColct is a list of paths which may contain the ORL name in it like this: ORL_msgtrait.xml.

So my question is: is there a way to merge the foreach I'm doing in my linq request to avoid having to retrieve all the xmls and then strip the unwanted ones?

Upvotes: 0

Views: 332

Answers (1)

Gilad Green
Gilad Green

Reputation: 37299

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(); 

But if you are already using Regex you can add to it that:

  1. It ends with ".xml"
  2. Contains "msgtrait" at the name area
  3. And also the Any part with some string.Join in order to form the pattern for the optional values of the colctNames

Upvotes: 1

Related Questions