0

我有一个项目清单让我们说| 1 | 2 | 3 | 4 | 5 | 我想取 1 并将其与列表的其余部分进行比较,但我不希望 1 与自身进行比较。在 1 与列表的其余部分进行比较后,我想将 1 弹出回列表中,然后取出 2 并这次与列表的其余部分进行比较,包括 1。这就是我尝试过的。这里的问题是,当我第一次迭代它时它工作得很好,因为第一次迭代从 0 开始并从 1 开始进行比较,但是一旦它循环超过 0 索引,它将开始与它进行比较,从而导致问题。任何帮助都是极好的。我知道这只是我必须弄清楚的逻辑问题,但我有点迷失了。

这是我尝试过的

        if (RadUpload1.UploadedFiles.Count > 1)
    {
        for (int fileBuffer = 0; fileBuffer < RadUpload1.UploadedFiles.Count; fileBuffer++)
        {
            for (int fileList = 1; fileList < RadUpload1.UploadedFiles.Count; fileList++)
            {
                if (RadUpload1.UploadedFiles[fileBuffer] != RadUpload1.UploadedFiles[fileList])
                {
                    if (RadUpload1.UploadedFiles[fileBuffer].GetName() == RadUpload1.UploadedFiles[fileList].GetName())
                    {
                        lbl_message.Text = "There where duplicates files found please check the files and try again";
                    }
                    else
                    {
                      //do other stuffs
                    }
4

5 回答 5

1
if (RadUpload1.UploadedFiles.Count > 1)
{
    for (int fileBuffer = 0; fileBuffer < RadUpload1.UploadedFiles.Count-1; fileBuffer++)
    {
        for (int fileList = fileBuffer + 1; fileList < RadUpload1.UploadedFiles.Count; fileList++)
        {
            if (RadUpload1.UploadedFiles[fileBuffer] != RadUpload1.UploadedFiles[fileList])
            {
//....
于 2013-08-22T15:33:21.690 回答
1

所以你想找到重复?LINQ 将大大简化它:

var list = new List<int> { 1, 2, 3, 4, 5 };
var dups = list.GroupBy(i => i).Where(g => g.Count() > 1);
foreach (var dup in dups)
{
    int i = dup.Key;
    int count = dup.Count();
}

这也适用于您的UploadedFileCollection(借助Cast它只实现非泛型CollectionBase类):

var dupFiles = uploadedFiles.Cast<UploadedFile>()
    .GroupBy(f => f.GetName())
    .Where(g => g.Count() > 1);
foreach (var dupFile in dupFiles)
{
    string fileName = dupFile.Key;
    int count = dupFile.Count();
}
于 2013-08-22T15:36:55.373 回答
1

我知道这不是使用您的代码,而是将每个项目与每个项目进行比较的简单概念,但它本身就在那里,祝你好运。

def search(alist):
    for anitem in alist:
        for anotheritem in alist:
            if alist.index(anitem) == alist.index(anotheritem):
                pass
            else:
                Do something
于 2013-08-22T15:46:15.747 回答
1

而不是从列表中删除和添加项目,您可以使用 Object.ReferenceEquals 来知道它是否是同一个 File 实例:

试试这个代码:

class Program
{
    static void Main(string[] args)
    {

        List<File> FileList = new List<File>();
        FileList.Add(new File { FullPath = "File1" });
        FileList.Add(new File { FullPath = "File2" });
        FileList.Add(new File { FullPath = "File3" });
        FileList.Add(new File { FullPath = "File4" });
        FileList.Add(new File { FullPath = "File5" });
        //FileList.Add(new FileName { FullPath = "File5" });

        foreach (File SourceFile in FileList) 
        {
            foreach (File TestFile in FileList) 
            {
                if (SourceFile.GetName() == TestFile.GetName() && !(Object.ReferenceEquals(SourceFile, TestFile)))
                {
                    var lbl_message = "There where duplicates files found please check the files and try again";
                }
                else 
                {
                    //another stuff
                }
            }                    
        }
    }
}

public class File
{
    public string FullPath;

    public string GetName()
    {
        return FullPath;
    }
}
于 2013-08-22T16:00:01.340 回答
0

您还可以为此使用 linq 查询:

    var duplicatesFiles = from file in FileList 
                     group file by file.GetName() into groupedbyname
                     where groupedbyname.Count() > 1
                     select groupedbyname.Count();

    bool thereAreDuplicates = duplicatesFiles.Count() > 0;
于 2013-08-22T16:27:28.973 回答