将两个类型不同是实体存入到一个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;
            }
        }
    }

 

posted on 2019-12-19 18:14  红磨坊后的白桦树  阅读(257)  评论(0编辑  收藏  举报