我有一个
dictionary<String,Object>
,我想将它转换为一个 List<Customer>
。有没有聪明的方法可以做到这一点?有什么例子吗?谢谢。
编辑
抱歉没有解释清楚。鉴于以下情况,为什么我的结果是0?请注意,我正在尝试模拟实时情况,第一个键没有意义,我想将其排除,只获取客户。为什么不起作用?感谢任何建议。class Program
{
static void Main(string[] args)
{
List<Customer> oldCustomerList = new List<Customer>
{
new Customer {Name = "Jo1", Surname = "Bloggs1"},
new Customer {Name = "Jo2", Surname = "Bloggs2"},
new Customer {Name = "Jo3", Surname = "Bloggs3"}
};
Dictionary<string,object>mydictionaryList=new Dictionary<string, object>
{
{"SillyKey", "Silly Value"},
{"CustomerKey", oldCustomerList}
};
List<Customer> newCustomerList = mydictionaryList.OfType<Customer>().ToList();
newCustomerList.ForEach(i=>Console.WriteLine("{0} {1}", i.Name, i.Surname));
Console.Read();
}
}
public class Customer
{
public string Name { get; set; }
public string Surname { get; set; }
}