How to iterate over this type of dictionary <int, Result>?

I have a bunch of results from a database that are stored in Dictionary<int, Result>().

I have a class of results:

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; }

So, it Dictionary<int, Result>has many results inside, and I would like to iterate over them. How am i doing this? I tried several methods, but even knew that they would not work.

I would like to do something like this:

foreach(var result in resultDict)
{
   mynewstring = result.Location;
   mynewstringtwo = result.Posted;
   // etc etc.
}
+3
source share
8 answers

Dcitionary has a ValueCollection called Values, so the code will be:

foreach (Result r in dict.Values)
{
    mynewstring = result.Location;
    mynewstringtwo = result.Posted;
}
+3
source
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
}

kvp KeyValuePair<int, Result>. Key Value, Dictionary Result, Key. /, , !; -)

@Wiktor, Values Keys, , int Value.

, :

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);
}
+7
var dictionary = ...;
foreach ( var result in dictionary.Values )
{
   ...
}

, ?

+1

Values a 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 KeyValuePair ( MSDN). 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
}
0
source

You can iterate over Dictionary.Values, which will look like any other IEnumerable<Result>

Or you can iterate over the dictionary, which is IEnumerable<KeyValuePair<int, Result>>

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

or the same thing, but using var:

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

^^ is the same, but var dynamically evaluates its own type

or you can do it if you just need the result:

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

Use foreachin dictionary:

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

0
source

All Articles