读书人

关于C# Contains的有关问题。 Contain

发布时间: 2012-10-18 13:46:55 作者: rapoo

关于C# Contains的问题。。急!!! Contains一直是false

C# code
public void parse()         {            for (int i = 0;i < Commond.fileInfo.Length ;i++ )            {                try                {                    if ((Commond.fileInfo[i].Name).Split('.')[1] != "csv") //判断是否csv文件                    {                        Console.WriteLine(Commond.fileInfo[i].Name + "此文件不是csv格式");                        continue;                    }                    using (StreamReader sr = new StreamReader(Commond.JLZDHdataPath + Commond.fileInfo[i].Name))                    {                        Console.WriteLine("开始解析" + Commond.fileInfo[i].Name);                        Commond.evenList = new List<csvEvent>();//初始化commond类中数据集合                        string s = "";//接收文本流                        string[] ss;//截取文本流返回的数组                        int index = 0;//检查是否有多个FID                        while ((s = sr.ReadLine()) != null)                        {                                csvEvent ce = new csvEvent();                                ss = s.Split(',');//截取文本流                                ce.Id = ss[0].Split('$')[1];                                ce.St = ss[2];                                ce.State = ss[3];                                ce.Gis_pb_id = equipment_Id(ce.Id);//查询equipmentid                                index = checkFid(ce.Id);                                if (index > 0)                                {                                    ce.Fid = selectFid(ce.Id);//查询Fid                                    bool a = Commond.evenList.Contains(ce);//判断evenList里面是否存在相同对象 问题是现在它一直等于false                                    if (a)                                        continue;//如果存在就continue                                    Commond.evenList.Add(ce);                                   }                                 Console.WriteLine(s);                        }                    }                    fillData();                    //填写日志--------------------                    RunLog2(System.Reflection.MethodBase.GetCurrentMethod().Name, "正常", Commond.fileInfo[i].Name);                    Commond.fileInfo[i].MoveTo(Commond.JLZDHdatabakPath + Commond.fileInfo[i].Name);//移动文件到文件夹                }                catch (Exception e)                {                    Console.WriteLine(e);                    //错误日志--------------------                    RunLog2(System.Reflection.MethodBase.GetCurrentMethod().Name, "错误", Commond.fileInfo[i].Name+e);                    Console.ReadKey(true);                    Environment.Exit(0);                                    }            }            Console.WriteLine("解析完毕!5分钟后将再次读取路径文件");        }

请各大神出手相救啊!!!!!
bool a = Commond.evenList.Contains(ce);//判断evenList里面是否存在相同对象 问题是现在它一直等于false


[解决办法]
ce 是new出来的,而commond中是已经存在的,比较也是比较的引用,所以new出来的和已经存在的不可能地址一样
你需要自己写个查找函数,来规定根据哪些内容判断是否一致

读书人网 >C#

热点推荐