转换dictionary.keyscollection到字符串数组 [英] convert dictionary.keyscollection to array of strings

查看:148
本文介绍了转换dictionary.keyscollection到字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个词典<字符串列表<排序>> ,我想有一个数组键的列表。但是,当我选择

i have a Dictionary<string, List<Order>> and i want to have the list of keys in an array. But when i choose

string[] keys = dictionary.Keys;

这不能编译。

如何转换为KeysCollection字符串数组。

how do i convert KeysCollection to array of strings.

推荐答案

假设你正在使用.NET 3.5或更高版本(使用System.Linq的; ):

Assuming you're using .NET 3.5 or later (using System.Linq;):

string[] keys = dictionary.Keys.ToArray();



否则,你将不得不使用CopyTo方法,或者使用一个循环:

Otherwise, you will have to use the CopyTo method, or use a loop :

string[] keys = new string[dictionary.Keys.Count];
dictionary.Keys.CopyTo(keys, 0);

这篇关于转换dictionary.keyscollection到字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆