首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用C#实现LINQ中的交集方法

用C#实现LINQ中的交集方法
EN

Stack Overflow用户
提问于 2011-06-29 02:14:36
回答 3查看 1.4K关注 0票数 4

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

代码语言:javascript
复制
Scores\Content\123.xml
Scores\Content\hi.xml
Scores\123.xml

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

这段代码来自C#代码

代码语言:javascript
复制
        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的代码

代码语言:javascript
复制
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();
    }

}

编辑:

代码语言:javascript
复制
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。正如我所说的,对于intersect方法,只有一个123.xml。

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

谢谢,

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-29 02:54:53

我刚刚更改了Equals方法以达到预期的结果。

代码语言:javascript
复制
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();
        }

    }
票数 3
EN

Stack Overflow用户

发布于 2011-06-29 02:24:09

一切看起来都很好。确保Scores\123.xmlPower\123.xml的长度相同。

票数 2
EN

Stack Overflow用户

发布于 2011-06-29 02:52:46

因为您只使用FileInfo.Name和FileInfo.Length进行比较,所以Content\123.xml与123.xml相同(假设两个文件具有相同的大小,我猜测试数据也是如此)。

因此,就您的FileCompare类而言,您的输入集合包含重复项。根据MSDN,Intersect计算每个集合中的不同的元素。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6511181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档