循环中有多个foreach循环

时间:2014-12-31 07:20:42

标签: c#

是否可以包含多个" foreach"任何循环结构内部的语句,如while或for ...我想同时从两个不同的目录打开.wav文件,以便我可以比较两者的文件。 这就是我想要的,但这肯定是错的......在这方面的任何帮助都表示赞赏。

string[] fileEntries1 = Directory.GetFiles(folder1, "*.wav");
string[] fileEntries2 = Directory.GetFiles(folder11, "*.wav");
while ( foreach(string fileName1 in fileEntries1) && foreach(string fileName2 in fileEntries2))

6 个答案:

答案 0 :(得分:2)

从字面上说不。这是因为foreach构造是语句,而while语句中的测试必须是表达式

最好的办法是嵌套foreach块:

foreach(string fileName1 in fileEntries1)
{
    foreach(string fileName2 in fileEntries2)

答案 1 :(得分:1)

我在一行中喜欢这种陈述。所以尽管这里的大部分答案都是正确的,但我还是给你了。

string[] fileEntries1 = Directory.GetFiles(folder1, "*.wav");
string[] fileEntries2 = Directory.GetFiles(folder11, "*.wav");

foreach( var fileExistsInBoth in fileEntries1.Where(fe1 => fileEntries2.Contains(fe1) )
{
    /// here you will have the records which exists in both of the lists
}

答案 2 :(得分:0)

如果要分别迭代两个路径中的所有文件对,可以按照以下步骤进行。

string[] fileEntries1 = Directory.GetFiles(folder1, "*.wav");
string[] fileEntries2 = Directory.GetFiles(folder11, "*.wav");

foreach(string fileName1 in fileEntries1)
{
    foreach(string fileName2 in fileEntries2)
    {
        // to the actual comparison
    }
}

答案 3 :(得分:0)

这是我建议的,使用linq

using System.Linq;

var fileEntries1 = Directory.GetFiles(folder1, "*.wav");
var fileEntries2 = Directory.GetFiles(folder11, "*.wav");
foreach (var entry1 in fileEntries1)
{
    var entries = fileEntries2.Where(x => Equals(entry1, x));
    if (entries.Any())
    {
        //We have matches
        //entries is a list of matches in fileentries2 for entry1
    }
}

答案 4 :(得分:0)

这样的事情,因为你只需要验证相同的文件名:

IEnumerable<string> fileEntries1 = Directory.GetFiles(folder1, "*.wav").Select(x => Path.GetFileName(x));
IEnumerable<string> fileEntries2 = Directory.GetFiles(folder2, "*.wav").Select(x => Path.GetFileName(x));

IEnumerable<string> filesToIterate = (fileEntries1.Count() > fileEntries2.Count()) ? fileEntries1 : fileEntries2;
IEnumerable<string> filesToValidate = (fileEntries1.Count() < fileEntries2.Count()) ? fileEntries1 : fileEntries2;

// Iterate the bigger collection
foreach (string fileName in filesToIterate)
{
    // Find the files in smaller collection
    if (filesToValidate.Contains(fileName))
    {
        // Get actual file and compare
    }
    else
    {
        // File does not exist in another list. Handle appropriately
    }
}

.Net 2.0解决方案:

List<string> fileEntries1 = new List<string>(Directory.GetFiles(folder1, "*.wav"));

List<string> fileEntries2 = new List<string>(Directory.GetFiles(folder2, "*.wav"));

List<string> filesToIterate = (fileEntries1.Count > fileEntries2.Count) ? fileEntries1 : fileEntries2;
filesToValidate = (fileEntries1.Count < fileEntries2.Count) ? fileEntries1 : fileEntries2;

string iteratorFileName;
string validatorFilePath;

// Iterate the bigger collection
foreach (string fileName in filesToIterate)
{
    iteratorFileName = Path.GetFileName(fileName);

    // Find the files in smaller collection
    if ((validatorFilePath = FindFile(iteratorFileName)) != null)
    {
        // Compare fileName and validatorFilePath files here
    }
    else
    {
        // File does not exist in another list. Handle appropriately
    }
}

FindFile方法:

static List<string> filesToValidate;

private static string FindFile(string fileToFind)
{
    string returnValue = null;

    foreach (string filePath in filesToValidate)
    {
        if (string.Compare(Path.GetFileName(filePath), fileToFind, true) == 0)
        {
            // Found the file
            returnValue = filePath;
            break;
        }
    }

    if (returnValue != null)
    {
        // File was found in smaller list. Remove this file from the list since we do not need to look for it again
        filesToValidate.Remove(returnValue);    
    }

    return returnValue;    
}

您可能会也可能不会根据需要选择使字段和方法保持静态。

答案 5 :(得分:0)

如果你想同时启用两个集合&#34;并且#34;,那么使用它们的迭代器:

var fileEntriesIterator1 = Directory.EnumerateFiles(folder1, "*.wav").GetEnumerator();
var fileEntriesIterator2 = Directory.EnumerateFiles(folder11, "*.wav").GetEnumerator();

while(fileEntriesIterator1.MoveNext() && fileEntriesIterator2.MoveNext())
{
    var file1 = fileEntriesIterator1.Current;
    var file2 = fileEntriesIterator2.Current;
}

如果一个集合比另一个集合短,那么当较短集合没有更多元素时,此循环将结束。