如何从此string
中获取所有Dictionary
值
class MyObject
{
public string MyString { get; set; }
public int MyInteger { get; set; }
}
Dictionary<int, MyObject> dictionary = new Dictionary<int, MyObject>();
如何将所有Mystring
放入string[]
?谢谢
您可以使用此:
string[] result = dictionary.Values.Select(x=> x.MyString).ToArray();
字典对象具有2个属性:Keys
和Values
。您可以根据需要使用它们。
Values
包含字典中所有值的集合。Select
用作投影,仅获得MyString
中的MyObject
属性。ToArray
扩展方法将结果转换为数组,作为问题中的预期结果。 string[] allStrings = dictionary.Values.ToArray();