list.OfType()
Posted mact
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了list.OfType()相关的知识,希望对你有一定的参考价值。
将两个类型不同是实体存入到一个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()的主要内容,如果未能解决你的问题,请参考以下文章