举个例子来使用哈希code检测一个名单,其中的一个元素;字符串>改变了C#字符串、举个例子、元素、名单

2023-09-06 22:14:13 作者:鬧够の錶γαη‰

我有一个列表,更新基于一些XML元素的LINQ查询的每一分钟。

在xml变化,不时。有人建议,我认为我可以使用哈希code,以确定是否有任何在列表中的字符串已经改变了。

我看到MD5哈希值code计算只是一个字符串的一些例子,而不是一个列表...可能有人告诉我有一个列表这样的一种方式?

我想简单的东西如int测试= list1.GetHash code;但是code是相同的,无论是什么列表...

这里是整个方法的链接查询和all..note的SequenceEqual底:

 私人无效GetTrackInfo()
    {
        _currentTitles1.Clear();
        VAR savedxmltracks =新的XDocument();

        listBox1.Items.Clear();
        Web客户端Web客户端=新的Web客户端();

        XmlDocument的xmltracks =新的XmlDataDocument();
        尝试
        {
            xmltracks.Load(_NPUrl);
            xmltracks.Save(xmltracks.xml);
        }
        赶上(WebException前)
        {
            StatusLabel1.Text = ex.Message;
        }

        尝试
        {
             savedxmltracks = XDocument.Load(xmltracks.xml);
        }
        赶上(例外前)
        {
            StatusLabel1.Text = ex.Message;
        }


        VAR dateQuery =从C在savedxmltracks.Descendants(内容)
                           选择C;

        。_count = savedxmltracks.Element(内容)元素(集合)属性(数)的价值。;

        VAR tracksQuery1 =从C在savedxmltracks.Descendants(数据)
                           选择新
                           {
                               标题= c.Attribute(标题),价值,
                               IMAGEURL = c.Attribute(图像),价值,
                               价格= c.Attribute(代价),价值,
                               说明= c.Attribute(产品描述​​),价值,
                               预选赛= c.Attribute(pricequalifier)。值

                           };

        VAR XML =新的XDocument(新XDeclaration(1.0,UTF-8,是),
        新的XElement(LastUsedSettings
            新的XElement(TimerInterval
                新的XElement(间隔,Convert.ToString(numericUpDown1.Value)))
            新的XElement(NowPlayingURL
                新的XElement(URL,_NPUrl)),
            新的XElement(电子邮件,EMAILADDRESS)
            新的XElement(LASTUPDATED,DateTime.Now.ToString())));
        的XElement StoreItems =新的XElement(StoreItems);


        INT I = 0;
        的foreach(在tracksQuery1变种C)
        {

            如果(c.title.Length&其中; = 40&安培; c.qualifier.Length&其中; = 12&安培;我小于10)
            {

                如果(c.title!= NULL)_title1 = c.title;
                如果(c.imageurl!= NULL)_imageUrl = c.imageurl;
                如果(c.price!= NULL)_price = c.price;
                如果(c.description!= NULL)_productDescription = c.description;
                如果(c.qualifier!= NULL)_priceQualifier = c.qualifier;
                //}
                StoreItems.Add(新的XElement(标题+ i.ToString(),_title1));
                _currentTitles1.Add(_title1);
                如果(_oldTitles1.Count大于0)
                {
                    Console.WriteLine(OldTitle:{0},新标题:{1},_oldTitles1 [I],_currentTitles1 [I]);
                }
                StoreItems.Add(新的XElement(价格+ i.ToString(),_price));
                StoreItems.Add(新的XElement(描述+ i.ToString(),_productDescription));
                StoreItems.Add(新的XElement(PriceQualifier+ i.ToString(),_priceQualifier));

                listBox1.Items.Add(标题:+ _title1);
                listBox1.Items.Add(图像URL:+ _imageUrl);
                listBox1.Items.Add(价格+ _price);
                listBox1.Items.Add(描述:+ _productDescription);
                listBox1.Items.Add(PriceQualifier:+ _priceQualifier);

                尝试
                {
                    为imageData = webClient.DownloadData(_imageUrl);
                }
                赶上(WebException前)
                {
                    StatusLabel1.Text = ex.Message;
                }

                MemoryStream的流=新的MemoryStream(为imageData);
                图片IMG = Image.FromStream(流);
                //图片saveimage = IMG;
                //saveimage.Save("pic.jpg);

                img.Save(PIC+ i.ToString()+.JPG);

                stream.Close();



                我++;
            }
        }



        //Console.WriteLine("Count:+ _count);
        Console.WriteLine(oldTitles数:+ _oldTitles1.Count.ToString());
        Console.WriteLine(currentTitles数:+ _currentTitles1.Count.ToString());

        如果(_oldTitles1.Count == 0)_oldTitles1 = _currentTitles1;

        如果(!_oldTitles1.SequenceEqual(_currentTitles1))
        {
            Console.WriteLine(项目改变了!);
            发送邮件();
            _oldTitles1 = _currentTitles1;
        }


        xml.Root.Add(StoreItems);
        xml.Save(的settings.xml);


    }
 

解决方案

我不认为你需要操心,所有的散列code讨论,如果你不打算有几十万的元素,或者您不会要求这个功能的第二数千次。

下面是一个小程序,它会告诉你需要多少时间需要使用这样的你正确的方法来比较10000元。

 类节目
{
    静态无效的主要(字串[] args)
    {
        VAR的List1 =新的名单,其中,串>();
        VAR list2中=新的名单,其中,串>();
        的for(int i = 0; I< 10000;我++)
        {
            list1.Add(有些很很很很很很很长的邮件+ I);
            list2.Add(有些很很很很很很很长的邮件+ I);
        }

        VAR定时器=新的秒表();
        timer.Start();
        list1.SequenceEqual(list2中);
        timer.Stop();
        Console.WriteLine(timer.Elapsed);
        Console.ReadKey();
    }
}
 
redis的数据结构 1 字符串类型2 哈希类型3 列表类型4 集合类型 5 有序集合类型详解

在我的电脑用了0.001秒。

I have a List that updates every minute based on a Linq query of some XML elements.

the xml changes, from time to time. It was suggested to me that I could use Hashcode to determine if any of the strings in the list have changed.

I have seen some examples of Md5 hashcode calculations for just a string, but not for a list...could someone show me a way of doing this with a list?

I tried something simple like int test = list1.GetHashCode; but the code is the same no matter what is in the list...

here is the entire method with the link query and all..note the SequenceEqual at the end:

        private void GetTrackInfo()
    {
        _currentTitles1.Clear();
        var savedxmltracks = new XDocument();

        listBox1.Items.Clear();
        WebClient webClient = new WebClient();

        XmlDocument xmltracks = new XmlDataDocument();
        try
        {
            xmltracks.Load(_NPUrl);
            xmltracks.Save("xmltracks.xml");
        }
        catch (WebException ex)
        {
            StatusLabel1.Text = ex.Message;
        }

        try
        {
             savedxmltracks = XDocument.Load("xmltracks.xml");
        }
        catch (Exception ex)
        {
            StatusLabel1.Text = ex.Message;
        }


        var dateQuery = from c in savedxmltracks.Descendants("content")
                           select c;

        _count = savedxmltracks.Element("content").Element("collection").Attribute("count").Value;

        var tracksQuery1 = from c in savedxmltracks.Descendants("data")
                           select new
                           {
                               title = c.Attribute("title").Value,
                               imageurl = c.Attribute("image").Value,
                               price = c.Attribute("price").Value,
                               description = c.Attribute("productdescription").Value,
                               qualifier = c.Attribute("pricequalifier").Value

                           };

        var xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
        new XElement("LastUsedSettings",
            new XElement("TimerInterval",
                new XElement("Interval", Convert.ToString(numericUpDown1.Value))),
            new XElement("NowPlayingURL",
                new XElement("URL", _NPUrl)),
            new XElement("Email", emailAddress),
            new XElement("LastUpdated", DateTime.Now.ToString())));
        XElement StoreItems = new XElement("StoreItems");


        int i = 0;
        foreach (var c in tracksQuery1)
        {

            if (c.title.Length <= 40 & c.qualifier.Length <= 12 & i < 10)
            {

                if (c.title != null) _title1 = c.title;
                if (c.imageurl != null) _imageUrl = c.imageurl;
                if (c.price != null) _price = c.price;
                if (c.description != null) _productDescription = c.description;
                if (c.qualifier != null) _priceQualifier = c.qualifier;
                //}
                StoreItems.Add(new XElement("Title" + i.ToString(), _title1));
                _currentTitles1.Add(_title1);
                if (_oldTitles1.Count > 0)
                {
                    Console.WriteLine("OldTitle: {0}, NewTitle: {1}", _oldTitles1[i], _currentTitles1[i]);
                }
                StoreItems.Add(new XElement("Price" + i.ToString(), _price));
                StoreItems.Add(new XElement("Description" + i.ToString(), _productDescription));
                StoreItems.Add(new XElement("PriceQualifier" + i.ToString(), _priceQualifier));

                listBox1.Items.Add("Title: " + _title1);
                listBox1.Items.Add("Image URL: " + _imageUrl);
                listBox1.Items.Add("Price: " + _price);
                listBox1.Items.Add("Description: " + _productDescription);
                listBox1.Items.Add("PriceQualifier: " + _priceQualifier);

                try
                {
                    imageData = webClient.DownloadData(_imageUrl);
                }
                catch (WebException ex)
                {
                    StatusLabel1.Text = ex.Message;
                }

                MemoryStream stream = new MemoryStream(imageData);
                Image img = Image.FromStream(stream);
                //Image saveimage = img;
                //saveimage.Save("pic.jpg");

                img.Save("pic" + i.ToString() + ".jpg");

                stream.Close();



                i++;
            }
        }



        //Console.WriteLine("Count: " + _count);
        Console.WriteLine("oldTitles Count: " + _oldTitles1.Count.ToString());
        Console.WriteLine("currentTitles Count: " + _currentTitles1.Count.ToString());

        if (_oldTitles1.Count == 0) _oldTitles1 = _currentTitles1;

        if (!_oldTitles1.SequenceEqual(_currentTitles1))
        {
            Console.WriteLine("Items Changed!");
            SendMail();
            _oldTitles1 = _currentTitles1;
        }


        xml.Root.Add(StoreItems);
        xml.Save("settings.xml");


    }

解决方案

I don’t think you need to bother yourself about all the hash code discussion if you are not going to have hundreds thousands of elements or if you are not going to request this function thousands times a second.

Here is a small program that will show you how much time it will take to compare 10000 element using your correct way of doing this.

class Program
{
    static void Main(string[] args)
    {
        var list1 = new List<string>();
        var list2 = new List<string>();
        for (int i = 0; i < 10000; i++)
        {
            list1.Add("Some very very very very very very very long email" + i);
            list2.Add("Some very very very very very very very long email" + i);
        }

        var timer = new Stopwatch();
        timer.Start();
        list1.SequenceEqual(list2);
        timer.Stop();
        Console.WriteLine(timer.Elapsed);
        Console.ReadKey();
    }
}

At my PC it took 0.001 seconds.