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

查看:140
本文介绍了防止重复列表< 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> 确实允许重复。

$ c> FileInfo ,包含方法将检查引用是否相同,但是当你正在获取一个完全 new 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天全站免登陆