There is a companyList list built on the basis of the query, it contains the names of companies, and there is a directoryList built on DirectoryInfo. As in linq, add a condition for matching directory names with names in the companyList list.

List<string> companyList = new List<string>(); using (SqlCommand addToCompanyList = new SqlCommand("SELECT [PROV_LATNAME] FROM [dbfTo1c].[dbo].[FileFormat]")) { addToCompanyList.Connection = new SqlConnection(Connection.dbfTo1c_ConectionString); addToCompanyList.Connection.Open(); SqlDataReader reader = addToCompanyList.ExecuteReader(); while (reader.Read()) companyList.Add(Convert.ToString(reader["PROV_LATNAME"])); //listBox1.DataSource = companyList; reader.Close(); addToCompanyList.Connection.Close(); } DirectoryInfo[] directoryList = new DirectoryInfo(path) .EnumerateDirectories() .Where(dirInfo => Regex.IsMatch(dirInfo.Name, searchP)) .ToArray(); 
  • yes as well, add one more Where - Grundy
  • There should be a strict match with the folder names, or the company name should be included in the folder name, in the second case, what to do if there are several search results? - Mirdin
  • one
    Possible duplicate question: Set whether the element is present in the array (C #) - Grundy
  • one
    In the case described by you, you can use the Intersect() method. something like var result = first.Select(x=>x.Name).Intersect(second.Select(x=>x.Name)) . - Bald

1 answer 1

In general, something like that)))

 DirectoryInfo[] directoryList = { }; List<DirectoryInfo> di = new DirectoryInfo(path).EnumerateDirectories().ToList<DirectoryInfo>(); directoryList = di.FindAll(x => Regex.IsMatch(x.Name.Replace("-" , "_") , searchP) && companyList.Exists(s => s == x.Name)).ToArray();