Net复习笔记:第七部分:集合

namespace NetNiBiXiuZhiDao

{

    public partial class WebForm13 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            List<UserLi> uer = new List<UserLi>();

            uer.Add(new UserLi() { Age = 1, Name = "Elong" });

            uer.Add(new UserLi() { Age = 3, Name = "Tuan" });

            uer.Add(new UserLi() { Age = 2, Name = "Hotel" });

             List排序方法:

            List<UserLi> st = uer.OrderBy(c => c.Age).ToList();

            ////List<UserLi> st = (from uu in uer

            ////                   orderby uu.Age

            ////                   select uu).ToList();

          List限制

             if (uer.Any(c => c.Age == 2))

             {

                 Response.Write("111");

             }

 

         //List去除重复

            // var stt = uer.Distinct(new ProductComparer());

            //uer.Union(uer1, new ProductComparer());

            //foreach (var s in stt)

            //{

            //    Response.Write(s.Age);

            //}

        }

    }

    class ProductComparer : IEqualityComparer<UserLi>

    {

        // Products are equal if their names and product numbers are equal.

        public bool Equals(UserLi x, UserLi y)

        {

            if (object.Equals(x.Age, y.Age))

            {

                return true;

            }

            else

            {

                return false;

            }

        }

        public int GetHashCode(UserLi obj)

        {

            return obj.Age;

        }

 

    }

    class UserLi

    {

        public int Age { get; set; }

        public string Name { get; set; }

    }

}

 

posted @ 2014-01-23 16:51  瀚海行舟  阅读(109)  评论(0编辑  收藏  举报