gpt4 book ai didi

c# - LINQ 与 C# 中的相交方法

转载 作者:行者123 更新时间:2023-11-30 14:17:02 25 4
gpt4 key购买 nike

我正在尝试使用 Intersect 方法从两个文件夹中获取相同的文件。123.xml 文件在所有文件夹中都是相同的(内容、日期、大小没有变化)。

Scores\Content\123.xml
Scores\Content\hi.xml
Scores\123.xml

Power\Content\123.xml
Power\Content\helo.xml
Power\123.xml

这是来自C#代码

        System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(path1);
System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(path2);

IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

FileCompare myFileCompare = new FileCompare();

bool areIdentical = list1.SequenceEqual(list2, myFileCompare);

if (areIdentical == true)
{
Console.WriteLine("the two folders are the same");
}
else
{
Console.WriteLine("The two folders are not the same");
}


var queryCommonFiles = list1.Intersect(list2, myFileCompare);

queryCommonFiles 只从 Content 文件夹返回 123.xml,而不是另一个。

这是来自 FileCompare 的代码

class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
{

public FileCompare() { }

public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
{
return (f1.Name == f2.Name &&
f1.Length == f2.Length);
}

// Return a hash that reflects the comparison criteria. According to the
// rules for IEqualityComparer<T>, if Equals is true, then the hash codes must
// also be equal. Because equality as defined here is a simple value equality, not
// reference identity, it is possible that two or more objects will produce the same
// hash code.
public int GetHashCode(System.IO.FileInfo fi)
{
string s = String.Format("{0}{1}", fi.Name, fi.Length);
return s.GetHashCode();
}

}

编辑:

var queryList1Only = (from file in list1
select file).Except(list2, myFileCompare);

Console.WriteLine("The following files are in list1 but not list2:\n");
foreach (var v in queryList1Only)
{
Console.WriteLine(v.FullName);
}


var queryList2Only = (from file in list2
select file).Except(list1, myFileCompare);

Console.WriteLine("The following files are in list2 but not list1:\n");
foreach (var v in queryList2Only)
{
Console.WriteLine(v.FullName);
}

这会为 list1 生成 hi.xml,为 list2 生成 helo.xml。正如我所说,相交方法只有一个 123.xml。

如有任何建议,我们将不胜感激

谢谢,

最佳答案

我只是更改了 Equals 方法以获得预期的结果

class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
{

public FileCompare() { }

public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
{
return (f1.Name == f2.Name && f1.Directory.Name == f2.Directory.Name &&
f1.Length == f2.Length);
}

// Return a hash that reflects the comparison criteria. According to the
// rules for IEqualityComparer<T>, if Equals is true, then the hash codes must
// also be equal. Because equality as defined here is a simple value equality, not
// reference identity, it is possible that two or more objects will produce the same
// hash code.
public int GetHashCode(System.IO.FileInfo fi)
{
string s = String.Format("{0}{1}", fi.Name, fi.Length);
return s.GetHashCode();
}

}

关于c# - LINQ 与 C# 中的相交方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6511181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com