0

How to retrieve first row from this dictionary. I put around some 15 records.

Dictionary<string, Tuple<string, string>> headINFO = 
                                   new Dictionary<string, Tuple<string, string>> { };
शेखर
  • 17,412
  • 13
  • 61
  • 117

5 Answers5

7

You can use headINFO.First(), but unless you know that there always will be at least one entry in headINFO I would recommend that you use headINFO.FirstOrDefault().

More information on the differences between the two available here.

Edit: Added simple examples

Here is a quick example on how to use FirstOrDefault()

var info = headINFO.FirstOrDefault().Value;
Console.WriteLine(info.Item1); // Prints Tuple item1
Console.WriteLine(info.Item2); // Prints Tuple item2

If you want to print multiple items you can use Take(x). In this case we will loop through three dictionary items, but you can easily modify the number to grab more items.

foreach (var info in headINFO.Take(3))
{
    Console.WriteLine(info.Value.Item1);
    Console.WriteLine(info.Value.Item2);
}

You should also keep in mind that the above foreach does not allow you to modify the values of your Dictionary entries directly.

Edit2: Clarified usage of First() and added clean foreach example

Keep in mind that while First() and FirstOrDefault() will provide you with a single item it does in no way guarantee that it will be the first item added.

Also, if you simply want to loop through all the items you can remove the Take(3) in the foreach loop mentioned above.

foreach (var info in headINFO)
{
    Console.WriteLine(info.Key);          // Print Dictionary Key
    Console.WriteLine(info.Value.Item1);  // Prints Turple Value 1
    Console.WriteLine(info.Value.Item2);  // Prints Turple Value 2
}
Community
  • 1
  • 1
eandersson
  • 25,781
  • 8
  • 89
  • 110
  • I have some 15 rows in the dictionary. from that I just want to retrieve each row separately and assign it to different local variables... eg) 1st String ID, 2nd String Head, 3rd STRING Content... SO now first row i want to assign it to these three varaiables... same way for second row and so on... – Vimalathithan Rajasekaran Mar 22 '13 at 02:54
5

Dictionaries are unordered, so there is no way to retrieve the first key-value pair you inserted.

You can retrieve an item by calling the LINQ .First() method.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
3

The easiest way is just to use Linq's First extension method:

var firstHead = headINFO.First();

Or if you want to be safer, the FirstOrDefault method will return null if the dictionary is empty:

var firstHead = headINFO.FirstOrDefault();

If you'd like to loop through all items in the dictionary, try this:

foreach(var head in headINFO)
{
    ...
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • @vimal you can do `.Skip(1).FirstOrDefault()`, but it sounds like you're going about this the wrong way. Can you elaborate on what you're trying to do? – p.s.w.g Mar 22 '13 at 02:48
  • I have some 15 rows in the dictionary. from that I just want to retrieve each row separately and assign it to different local variables... eg) 1st String ID, 2nd String Head, 3rd STRING Content... SO now first row i want to assign it to these three varaiables... same way for second row and so on... – Vimalathithan Rajasekaran Mar 22 '13 at 02:52
  • whatever you are trying to do, but doesn't sound something good. – Dilshod Mar 22 '13 at 02:56
  • 1
    @vimal you probably want to use a `foreach` loop, or better yet, you may want to rethink using a dictionary at all -- whatever you're doing, there's probably a better way of doing it. – p.s.w.g Mar 22 '13 at 03:01
2

Try this code.

headINFO.FirstOrDefault();
Esha Garg
  • 144
  • 8
-1

Try this:

    int c=0;

    foreach(var item in myDictionary)
    {
         c++;
         if(c==1)
         {
             myFirstVar = item.Value;
         }
         else if(c==2)
         {
             mySecondVar = item.Value;
         }
          ......
    }
Dilshod
  • 3,189
  • 3
  • 36
  • 67