C# 将词典转换为列表<;客户>;

C# 将词典转换为列表<;客户>;,c#,C#,我有一本字典,我想把它转换成列表 有没有聪明的方法? 有什么例子吗? 谢谢 已编辑 很抱歉没有解释清楚。 鉴于以下原因,为什么我的结果是0? 请注意,我试图模拟一个真实的情况,第一个关键点没有意义,希望排除,所以只有我应该得到的客户。 为什么它不起作用?谢谢你的建议 class Program { static void Main(string[] args) { List<Customer> oldCustomerList = new List<

我有一本
字典
,我想把它转换成
列表
有没有聪明的方法? 有什么例子吗? 谢谢

已编辑

很抱歉没有解释清楚。 鉴于以下原因,为什么我的结果是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; }
}
类程序
{
静态void Main(字符串[]参数)
{
列表oldCustomerList=新列表
{
新客户{Name=“Jo1”,姓氏=“Bloggs1”},
新客户{Name=“Jo2”,姓氏=“Bloggs2”},
新客户{Name=“Jo3”,姓氏=“Bloggs3”}
};
DictionaryDictionaryList=新字典
{
{“傻键”,“傻值”},
{“CustomerKey”,旧CustomerList}
};
List newCustomerList=mydictionaryList.OfType().ToList();
newCustomerList.ForEach(i=>Console.WriteLine(“{0}{1}”,i.Name,i.姓氏));
Console.Read();
}
}
公共类客户
{
公共字符串名称{get;set;}
公共字符串姓氏{get;set;}
}

肯定有很多方法可以做到这一点,但是您没有提到客户中有什么,或者字符串、对象和客户之间的关系是什么

下面是一个可能合适的示例(假设您使用的是.NET 3.5或更高版本):

或者你只对钥匙感兴趣,钥匙应该是客户的名字:

var customers = dictionary.Keys.Select(x => new Customer(x))
                               .ToList();
或者可能每个值都已经是
客户
,但您需要强制转换:

var customers = dictionary.Values.Cast<Customer>().ToList();
(您也可以使用
List
的构造函数,它接受
IEnumerable
,但我倾向于认为
ToList
扩展方法更具可读性。)


编辑:好的,现在我们知道了要求,选项是:

List<Customer> customers = dictionary.Values.OfType<List<Customer>>()
                                            .First();
List customers=dictionary.Values.OfType()
.First();

List customers=dictionary.Values.OfType()
.FirstOrDefault();

如果没有这样的值,后者将给您留下
null
;前者将引发异常。

给定更新的代码,列表中的相关对象是一个
列表
,因此您应该使用OfType进行检查。试着这样从字典中的所有列表中形成一个列表

var newList = mydictionaryList.Values.OfType<List<Customer>>().SelectMany(list => list).ToList();
var newList=mydictionaryList.Values.OfType().SelectMany(list=>list.ToList();

否则,您可能会得到一个列表列表

你字典里的对象是顾客吗?客户是如何构成的?听起来你想要一个LINQ select,但不知道你想要你的客户是什么,我们不知道它应该是什么样子。我发誓他就像一个代表黑洞。没有人能逃脱他的引力。而且,如果他的TValue可能是客户,也可能不是客户(如果他保留了多个类型作为值),那么使用类型比使用强制转换更合适。@Will:捕捉得好。我将添加该选项。我已经编辑了我的问题,解释了更多,并进行了测试。我似乎无法从中得到结果。我做错了什么?@devnet247:在您的测试用例中,字典已经包含列表本身作为一个值。您只想选择字典中出现的第一个列表吗?或者将列表组合在一起?嗨,它在我的noddy示例中有效,但是在我的真实示例中,one by object由其他对象组成。发布所有代码有点复杂,我不确定是否可以。简言之。如果我用foreach按惯例做这件事,你会怎么做。很遗憾,它与example posted.mmmmm一起工作。现在有点迷失了
List<Customer> customers = dictionary.Values.OfType<List<Customer>>()
                                            .First();
List<Customer> customers = dictionary.Values.OfType<List<Customer>>()
                                            .FirstOrDefault();
var newList = mydictionaryList.Values.OfType<List<Customer>>().SelectMany(list => list).ToList();