码迷,mamicode.com
首页 > 其他好文 > 详细

list.OfType()

时间:2019-12-19 19:31:16      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:object   魅族   lse   美国   style   北京   class   lists   new   

将两个类型不同是实体存入到一个object可以通过OfType方法分别取出来

//将两个类型不同是实体存入到一个object可以通过OfType方法分别取出来
            List<Phone> PhoneLists = new List<Phone>()
            {
                new Phone { Country = "中国", City = "北京", Name = "小米" },
                new Phone { Country = "中国",City = "北京",Name = "华为"},
                new Phone { Country = "中国",City = "北京",Name = "联想"},
                new Phone { Country = "中国",City = "台北",Name = "魅族"},
                new Phone { Country = "日本",City = "东京",Name = "索尼"},
                new Phone { Country = "日本",City = "大阪",Name = "夏普"},
                new Phone { Country = "日本",City = "东京",Name = "松下"},
                new Phone { Country = "美国",City = "加州",Name = "苹果"},
                new Phone { Country = "美国",City = "华盛顿",Name = "三星"},
                new Phone { Country = "美国",City = "华盛顿",Name = "HTC"}

            };
            var Lists = PhoneLists.Select(p => new GetMyPhone().MyPhone(p)).ToList();
            var myLists = Lists.OfType<MyPhone>().ToList();
            var myList2 = Lists.OfType<Phone>().ToList();
 public class Phone
    {
        public string Country { get; set; }
        public string City { get; set; }
        public string Name { get; set; }
    }

    public class MyPhone
    {
        public string MyCity { get; set; }
        public string MyName { get; set; }
    }

    public class GetMyPhone
    {
        public object MyPhone(Phone phone)
        {
            if (phone.Country.Equals("中国"))
            {
                MyPhone myphone = new MyPhone()
                {
                    MyCity = phone.City,
                    MyName = phone.Name
                };
                return myphone;
            }
            else
            {
                return phone;
            }
        }
    }

 

list.OfType()

标签:object   魅族   lse   美国   style   北京   class   lists   new   

原文地址:https://www.cnblogs.com/macT/p/12069362.html

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