我有一堆来自数据库的结果,我保存在 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.
}

最佳答案

字典有一个叫做 Values 的 ValueCollection,所以代码是:

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

10-08 08:50
查看更多