繁体   English   中英

如何迭代这种特殊类型的Dictionary <int, Result> ?

[英]How to iterate over this particular type of Dictionary<int, Result>?

我有一堆来自数据库的结果,我保存在Dictionary<int, Result>()

我有的Result类是:

public int Id { get; set; }
public string something { get; set; }
public string somethingtwo { get; set; }
public string somethingthree { get; set; }
public DateTime Posted { get; set; }
public string Location { get; set; }
public bool somethingfour { get; set; }

所以, Dictionary<int, Result>里面有很多结果,我想迭代它们。 我是怎么做到的? 我尝试了几种方法,但即使我知道它们也行不通。

我想做这样的事情:

foreach(var result in resultDict)
{
   mynewstring = result.Location;
   mynewstringtwo = result.Posted;
   // etc etc.
}
foreach(var kvp in resultsDict) {
    //Do somethign with kvp
    UseResult(kvp.Value); //kvp.Value is a Result object
    UseKey(kvp.Key); //kvp.Key is the integer key you access it with
}

在上面的代码中, kvpKeyValuePair<int, Result> 您可以访问“ Key和“ Value属性以获取“ Dictionary的整数键和与该Key关联的“ Result值。 我会把它作为一个运动/猜谜游戏让你弄清楚哪个属性是哪个! ;-)

正如@Wiktor所提到的,您还可以访问字典的ValuesKeys集合来执行相同的操作,但是检索一系列intValue属性。

使用其他集合的替代方案:

foreach(var val in resultsDict.Values) {
    // The key is not accessible immediately,
    // you have to examine the value to figure out the key
    UseResult(val); //val is a value.
}

foreach(var key in resultsDict.Keys) {
    //The value isn't directly accessible, but you can look it up.
    UseResult(resultsDict[key]);
    UseKey(key);
}

Dcitionary有一个名为Values的ValueCollection,所以代码是:

foreach (Result r in dict.Values)
{
    mynewstring = result.Location;
    mynewstringtwo = result.Posted;
}
var dictionary = ...;
foreach ( var result in dictionary.Values )
{
   ...
}

这就是你需要的吗?

您可以使用Values属性迭代Dictionary的值(另请参阅MSDN页面 )。

代码示例:

// Your dictionary
var myResultDictionary = new Dictionary<int, Result>();

foreach (var item in myResultDictionary.Values)
{
    // "item" holds a Result object
    // Do something with item
}

您还可以定期循环遍历Dictionary ,但是item将是KeyValuePairMSDN页面 )对象。 您可以使用变量项上的Value属性访问该值。

代码示例:

// Your dictionary
var myResultDictionary = new Dictionary<int, Result>();

foreach (var item in myResultDictionary)
{
    // "item" holds a KeyValuePair<int, Result> object
    // You can access the value (a Result object) by calling "item.Value"
    // Do something with item
}

您可以迭代Dictionary.Values,这将像任何其他IEnumerable<Result>

或者,您可以遍历Dictionary,这是一个IEnumerable<KeyValuePair<int, Result>>

foreach (KeyValuePair<int,Result> item in dictionary)
            {
                //do stuff
            }
foreach(KeyValuePair<int,result> keyValue in yourDictionary)
{
    Debug.WriteLine(keyValue.Value);
    //or
    Debug.WriteLine(keyValue.Key);
}

或同样的事情,但使用var:

foreach(var keyValue in yourDictionary)
{
    Debug.WriteLine(keyValue.Value);
    //or
    Debug.WriteLine(keyValue.Key);
}

^^同样的事情,但var动态地计算出它自己的类型

或者你可以这样做,如果你只需要结果:

foreach(var result in yourDictionary.Values)
{
    Debug.WriteLine(result);
}

在词典上使用foreach

foreach (var keyValue in dictionary) { //Use key value here }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM