Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从列表中清除重复项及其实例_C#_List - Fatal编程技术网

C# 从列表中清除重复项及其实例

C# 从列表中清除重复项及其实例,c#,list,C#,List,我有这样一个数据模型: public class AmpFile { public string filename { get; set; } public string actualpath { get; set; } } [ list member 1 ] - filename: "testfile1.jpg" - actualpath: "C:\testpath\testfile1.jpg" [ lis

我有这样一个数据模型:

    public class AmpFile
    {
        public string filename { get; set; }
        public string actualpath { get; set; }
    }
[ list member 1 ]    -    filename:  "testfile1.jpg"    -    actualpath:  "C:\testpath\testfile1.jpg" 
[ list member 2 ]    -    filename:  "brickwall.jpg"    -    actualpath:  "C:\testpath\brickwall.jpg" 
[ list member 3 ]    -    filename:  "mydata.txt"    -    actualpath:  "D:\mydata.txt" 
[ list member 4 ]    -    filename:  "testfile1.jpg"    -    actualpath:  "E:\demo\testfile1.jpg" 
[ list member 5 ]    -    filename:  "mydata.txt"    -    actualpath:  "F:\somefolder\mydata.txt" 
[ list member 6 ]    -    filename:  "testfile1.jpg"    -    actualpath:  "F:\somefolder\testfile1.jpg" 
[ list member 7 ]    -    filename:  "testfile2.jpg"    -    actualpath:  "F:\somefolder\testfile2.jpg" 
[ list member 7 ]    -    filename:  "testfile3.jpg"    -    actualpath:  "D:\testfile3.jpg" 
现在我有一个这样的列表:

    public class AmpFile
    {
        public string filename { get; set; }
        public string actualpath { get; set; }
    }
[ list member 1 ]    -    filename:  "testfile1.jpg"    -    actualpath:  "C:\testpath\testfile1.jpg" 
[ list member 2 ]    -    filename:  "brickwall.jpg"    -    actualpath:  "C:\testpath\brickwall.jpg" 
[ list member 3 ]    -    filename:  "mydata.txt"    -    actualpath:  "D:\mydata.txt" 
[ list member 4 ]    -    filename:  "testfile1.jpg"    -    actualpath:  "E:\demo\testfile1.jpg" 
[ list member 5 ]    -    filename:  "mydata.txt"    -    actualpath:  "F:\somefolder\mydata.txt" 
[ list member 6 ]    -    filename:  "testfile1.jpg"    -    actualpath:  "F:\somefolder\testfile1.jpg" 
[ list member 7 ]    -    filename:  "testfile2.jpg"    -    actualpath:  "F:\somefolder\testfile2.jpg" 
[ list member 7 ]    -    filename:  "testfile3.jpg"    -    actualpath:  "D:\testfile3.jpg" 
现在我想找到每个成员的重复项,如果有重复项,我想删除重复项+引用本身,因此我想要实现的结果是:

[ list member 1 ]    -    filename:  "brickwall.jpg"    -    actualpath:  "C:\testpath\brickwall.jpg" 
[ list member 2 ]    -    filename:  "testfile2.jpg"    -    actualpath:  "F:\somefolder\testfile2.jpg" 
[ list member 3 ]    -    filename:  "testfile3.jpg"    -    actualpath:  "D:\testfile3.jpg" 

我该怎么做呢?

如果你不想得到一个新的列表,而不是从原始列表中删除,你可以这样做(很抱歉太复杂了,我认为它很容易优化)→ 添加中断等…:

listfoo(列表文件)
{
列表结果=新列表();
bool add=false;
foreach(文件中的AmpFile文件)
{
加法=真;
foreach(结果中已添加AmpFile)
{
if(file.filename==alreadyAdded.filename)
{
add=false;
}
}
如果(添加)
{
结果.添加(文件);
}
}
返回结果;
}
如果确实需要更改原始列表,可以执行以下操作(可以再次优化):

void foo2(列出文件)
{
AmpFile[]temp=files.ToArray();
List toDelete=新列表();
foreach(temp中的AmpFile文件)
{
foreach(文件中的AMPF文件)
{
如果(f!=file&&f.filename==file.filename)
{
如果(!toDelete.Contains(f))
{
添加(f);
}
}
}
}
foreach(toDelete中的AmpFile文件)
{
文件。删除(文件);
}
}

在列表中运行两个循环是最快的方法

List<AmpFile> ampList = new List<AmpFile>();
// Populate list

for (int i = 0; i < ampList.Count; i++)
    for (int j = i + 1; j < ampList.Count; j++)
        if (ampList[j].filename == ampList[i].filename)
            ampList.RemoveAt(j);
List-ampList=新列表();
//填充列表
对于(int i=0;i
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序1
{
公共类文件
{
公共字符串文件名{get;set;}
公共字符串实际路径{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
List lstemail=新列表();
添加(新的AmpFile{filename=“testfile1.jpg”,actualpath=“C:\testpath\testfile1.jpg”});
添加(新的AmpFile{filename=“brickwall.jpg”,actualpath=“C:\testpath\brickwall.jpg”});
添加(新的AmpFile{filename=“mydata.txt”,actualpath=@“D:\mydata.txt”});
添加(新的AmpFile{filename=“testfile1.jpg”,actualpath=@“E:\demo\testfile1.jpg”});
var myDistinctList=lstemail.GroupBy(i=>
i、 选择(g=>g.First()).ToList();
lstemail=myDistinctList;
}
}
}

我使用linq比使用foreach更好。

您可以使用linq,通过使用并过滤所有计数=1的元素,如以下代码:
1-准备
AMP文件列表

List<AmpFile> ampFiles = new List<AmpFile>
{
    new AmpFile{filename="testfile1.jpg",actualpath="C:\\testpath\\testfile1.jpg"},
    new AmpFile{filename="brickwall.jpg",actualpath="C:\\testpath\\brickwall.jpg"},
    new AmpFile{filename="mydata.txt",actualpath="D:\\mydata.txt"},
    new AmpFile{filename="testfile1.jpg",actualpath="E:\\demo\testfile1.jpg"},
    new AmpFile{filename="mydata.txt",actualpath="F:\\somefolder\\mydata.txt"},
    new AmpFile{filename="testfile1.jpg",actualpath="F:\\somefolder\\testfile1.jpg"},
    new AmpFile{filename="testfile2.jpg",actualpath="F:\\somefolder\\testfile2.jpg"},
    new AmpFile{filename="testfile3.jpg",actualpath="D:\\testfile3.jpg"},
};
3-演示:

foreach(AmpFile ampFile in notDuplicatedAmpFiles)
{
    Console.WriteLine($"fileName :{ampFile.filename}, actualPath :{ampFile.actualpath}");
}
4-结果:

fileName :brickwall.jpg, actualPath :C:\testpath\brickwall.jpg
fileName :testfile2.jpg, actualPath :F:\somefolder\testfile2.jpg
fileName :testfile3.jpg, actualPath :D:\testfile3.jpg

我希望这能有所帮助。

您可以使用类似下面代码的equals。您的路径位于不同的文件夹中,因此您没有任何重复的路径。见下文:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<AmpFile> files = new List<AmpFile>() {
                new AmpFile() { filename = "testfile1.jpg", actualpath = @"C:\testpath\testfile1.jpg"}, 
                new AmpFile() { filename = "brickwall.jpg", actualpath = @"C:\testpath\brickwall.jpg"}, 
                new AmpFile() { filename = "mydata.txt", actualpath = @"D:\mydata.txt"}, 
                new AmpFile() { filename = "testfile1.jpg", actualpath = @"E:\demo\testfile1.jpg"}, 
                new AmpFile() { filename = "mydata.txt", actualpath = @"F:\somefolder\mydata.txt"}, 
                new AmpFile() { filename = "testfile1.jpg" , actualpath = @"F:\somefolder\testfile1.jpg"}, 
                new AmpFile() { filename = "testfile2.jpg" , actualpath = @"F:\somefolder\testfile2.jpg"}, 
                new AmpFile() { filename = "testfile3.jpg", actualpath = @"D:\testfile3.jpg"}
            };

            List<AmpFile> output = files.Distinct().ToList();
        }
    }
    public class AmpFile : IEquatable<AmpFile>
    {
        public string filename { get; set; }
        public string actualpath { get; set; }

        public Boolean Equals(AmpFile other)
        {
            return ((this.filename == other.filename) && (this.actualpath == other.actualpath));
        }
        public override int GetHashCode()
        {
            return (this.filename + "^" + this.actualpath).GetHashCode();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
列表文件=新列表(){
新的AmpFile(){filename=“testfile1.jpg”,actualpath=@“C:\testpath\testfile1.jpg”},
新建AmpFile(){filename=“brickwall.jpg”,actualpath=@“C:\testpath\brickwall.jpg”},
新建AmpFile(){filename=“mydata.txt”,actualpath=@“D:\mydata.txt”},
新建AmpFile(){filename=“testfile1.jpg”,实际路径=@“E:\demo\testfile1.jpg”},
新建AmpFile(){filename=“mydata.txt”,actualpath=@“F:\somefolder\mydata.txt”},
新建AmpFile(){filename=“testfile1.jpg”,actualpath=@“F:\somefolder\testfile1.jpg”},
新建AmpFile(){filename=“testfile2.jpg”,actualpath=@“F:\somefolder\testfile2.jpg”},
新建AmpFile(){filename=“testfile3.jpg”,实际路径=@“D:\testfile3.jpg”}
};
列表输出=files.Distinct().ToList();
}
}
公共类AmpFile:IEquatable
{
公共字符串文件名{get;set;}
公共字符串实际路径{get;set;}
公共布尔等于(AmpFile other)
{
返回((this.filename==other.filename)&&(this.actualpath==other.actualpath));
}
公共覆盖int GetHashCode()
{
返回(this.filename+“^”+this.actualpath).GetHashCode();
}
}
}

我建议您进行以下查询:

var results =
    from a in list
    group a by a.filename into gas
    where !gas.Skip(1).Any()
    from ga in gas.Take(1)
    select ga;
如果从该数据开始:

var list = new List<AmpFile>()
{
    new AmpFile() { filename = "testfile1.jpg", actualpath = @"C:\testpath\testfile1.jpg" },
    new AmpFile() { filename = "brickwall.jpg", actualpath = @"C:\testpath\brickwall.jpg" },
    new AmpFile() { filename = "mydata.txt", actualpath = @"D:\mydata.txt" },
    new AmpFile() { filename = "testfile1.jpg", actualpath = @"E:\demo\testfile1.jpg" },
    new AmpFile() { filename = "mydata.txt", actualpath = @"F:\somefolder\mydata.txt" },
    new AmpFile() { filename = "testfile1.jpg", actualpath = @"F:\somefolder\testfile1.jpg" },
    new AmpFile() { filename = "testfile2.jpg", actualpath = @"F:\somefolder\testfile2.jpg" },
    new AmpFile() { filename = "testfile3.jpg", actualpath = @"D:\testfile3.jpg" },
};

var list=new list

你能说得更清楚一点吗,你把什么叫做“复制品”(相同的文件名?相同的路径?两者都有?)?你想删除什么(只有副本?副本和原件?@kiliz是的,我想找到重复的文件名,如testfile1.jpg,而不关心地址,并删除副本和原始引用(首先在列表中找到…),请不要发布类似
[列表成员1]-文件名:“testfile1.jpg”-实际路径:“C:\testpath\testfile1.jpg”
当有效的C#将允许我们编写和测试代码以获得答案时。使用数字索引器而不是枚举器可以更快地完成此任务。我最近使用了太多的IENUERATOR,所以我有点格式化,抱歉!^ 7
foreach(Ampfile:files)
。在发布之前,你应该测试所有你能测试的东西。看看这里的答案,对for vs foreach进行深入讨论:现在,更麻烦的是,OP的类型是
AmpFile
,但是你已经使用了
AmpFile
。1)你会得到一些警告,因为你在枚举列表时正在修改它。2)
。Length
你要做什么esn不存在于列表中,请使用
.Count
。但总体思路是很好地捕获计数,我是从内存中编写的。您不会收到警告,因为使用索引器不会枚举。您在调用时正在枚举列表
var results =
    from a in list
    group a by a.filename into gas
    where !gas.Skip(1).Any()
    from ga in gas.Take(1)
    select ga;
var list = new List<AmpFile>()
{
    new AmpFile() { filename = "testfile1.jpg", actualpath = @"C:\testpath\testfile1.jpg" },
    new AmpFile() { filename = "brickwall.jpg", actualpath = @"C:\testpath\brickwall.jpg" },
    new AmpFile() { filename = "mydata.txt", actualpath = @"D:\mydata.txt" },
    new AmpFile() { filename = "testfile1.jpg", actualpath = @"E:\demo\testfile1.jpg" },
    new AmpFile() { filename = "mydata.txt", actualpath = @"F:\somefolder\mydata.txt" },
    new AmpFile() { filename = "testfile1.jpg", actualpath = @"F:\somefolder\testfile1.jpg" },
    new AmpFile() { filename = "testfile2.jpg", actualpath = @"F:\somefolder\testfile2.jpg" },
    new AmpFile() { filename = "testfile3.jpg", actualpath = @"D:\testfile3.jpg" },
};