码迷,mamicode.com
首页 > Windows程序 > 详细

[C#]SequenceEqual使用小结

时间:2014-12-15 16:44:11      阅读:933      评论:0      收藏:0      [点我收藏+]

标签:des   style   ar   color   os   使用   sp   java   on   

示例:

        [TestMethod()]
        public void DeserializeTest()
        {
            Person _personA = new Person() { Name = "YanZhiweiA", Age = 10, Address = "shanghaiA" };
            Person _personB = new Person() { Name = "YanZhiweiB", Age = 11, Address = "shanghaiB" };
            List<Person> _expected = new List<Person>();
            _expected.Add(_personA);
            _expected.Add(_personB);

            string _jsonString = "[{‘Name‘:‘YanZhiweiA‘,‘Age‘:10,‘Address‘:‘shanghaiA‘},{‘Name‘:‘YanZhiweiB‘,‘Age‘:11,‘Address‘:‘shanghaiB‘}]";
            List<Person> _result = (List<Person>)JavaScriptSerializerHelper.Deserialize<Person>(_jsonString);
            bool _actual = _expected.SequenceEqual(_result, new PersonCompare());
            Assert.IsTrue(_actual);
        }
    public class PersonCompare : IEqualityComparer<Person>
    {
        public bool Equals(Person x, Person y)
        {
            return (x.Age == y.Age) && (x.Address == y.Address) && (x.Name == y.Name);
        }
        public int GetHashCode(Person obj)
        {
            return obj.Name.GetHashCode();

        }
    }

说明:注意黄色背景部分代码,这样子可以利用SequenceEqual很好比较集合,希望有所帮助,谢谢!

[C#]SequenceEqual使用小结

标签:des   style   ar   color   os   使用   sp   java   on   

原文地址:http://www.cnblogs.com/Yan-Zhiwei/p/4164890.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!