加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

LINQ中与C#的Intersect方法

发布时间:2020-12-16 01:45:07 所属栏目:百科 来源:网络整理
导读:我试图从两个文件夹使用Intersect方法获取相同的文件. 所有文件夹中的123.xml文件都是相同的(内容,日期,大小没有变化). ScoresContent123.xmlScoresContenthi.xmlScores123.xmlPowerContent123.xmlPowerContenthelo.xmlPower123.xml 这是来自C#代
我试图从两个文件夹使用Intersect方法获取相同的文件.
所有文件夹中的123.xml文件都是相同的(内容,日期,大小没有变化).

ScoresContent123.xml
ScoresContenthi.xml
Scores123.xml

PowerContent123.xml
PowerContenthelo.xml
Power123.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>,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.Length);
            return s.GetHashCode();
        }

    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读