防止重复列表< T>项 [英] Preventing Duplicate List<T> Entries

查看:117
本文介绍了防止重复列表< T>项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我能够做一个工作,但是我无法理解为什么这段代码无法正常运行,并允许将重复的条目添加到列表中。

I expect I'll be able to make a work around but I can't for the life of me understand why this code is not functioning correctly and allowing duplicate entries to be added to the List.

即使我从相同的位置拖拽相同的文件,也不会满足 if 语句条件。我不明白为什么包含方法与它们不匹配。

The if statement condition is never met, even when I drag identical files in from the same location. I don't understand why the "Contains" method isn't matching them up.

public class Form1:Form {
    private List<FileInfo> dragDropFiles = new List<FileInfo>();

    private void Form1_DragDrop(object sender, DragEventArgs e) {
        try {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
                string[] files =
                    (string[])e.Data.GetData(DataFormats.FileDrop);

                OutputDragDrop(files);
            }
        }
        catch { }
    }

    private void Form1_DragEnter(object sender, DragEventArgs e) {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effect = DragDropEffects.Copy;
        else
            e.Effect = DragDropEffects.None;
    }

    private void OutputDragDrop(string[] files) {
        try {
            foreach (string file in files) {
                FileInfo fileInfo = new FileInfo(file);

                if (dragDropFiles.Contains(fileInfo)) {
                    dragDropFiles.Remove(fileInfo);
                }
                dragDropFiles.Add(fileInfo);
            }
            PopulateContextMenu();
        }
        catch { }
    }
}

我以为我已经找到了另一种方法来实现这个使用Distinct

I thought I had found another method in which to achieve this using "Distinct"

但是,它出现 checkedDragDropFiles &安培; dragDropFiles 具有相同的条目数量,包括重复项,除非 dragDropFiles 显示在 ListBox 它不显示它们。为什么会这样做?

However, it appears checkedDragDropFiles & dragDropFiles have the same amount of entries, including duplicates, except when dragDropFiles is displayed in a ListBox it doesn't show them. Why does it do this?

我需要防止任何重复的列表条目,因为我将以编程方式创建基于列表数据的菜单。

I need to prevent any duplicated list entries, as I would be programmatically creating a menu based off of the list data.

private void OutputDragDrop(string[] files)
{
    try
    {
        foreach (string file in files)
        {
            FileInfo fileInfo = new FileInfo(file);

            //if (dragDropFiles.Contains(fileInfo))
            //{
            //    dragDropFiles.Remove(fileInfo);
            //}
            dragDropFiles.Add(fileInfo);
        }

        List<FileInfo> checkedDragDropFiles = dragDropFiles.Distinct().ToList();

        debugList.DataSource = checkedDragDropFiles;
        debugList2.DataSource = dragDropFiles;
        //PopulateContextMenu();
    }
    catch { }
}


推荐答案

列表< T> 确实允许重复。

FileInfo 包含方法将检查引用是否相同,但是您正在获取一个完全 $ FileInfo ,引用是不同的。

In the case of FileInfo, the Contains method will be checking whether the references are the same, but as you are fetching a completely new set of FileInfo, the references are different.

你需要使用包含,它需要一个 IEqualityComparer - 请参阅这里

You need to use the overload of Contains that takes an IEqualityComparer - see here.

您还可以使用 HashSet< T> - 它是不允许重复的数据结构(虽然有不同的引用,你仍然会有这个问题)。

You can also use HashSet<T> instead - it is a data structure that does not allow duplicates (though with different references, you will still have this issue).

这篇关于防止重复列表&lt; T&gt;项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆