bmt22033
bmt22033

Reputation: 7240

Get first KeyValuePair in a dictionary that has a specific value

I'm writing an app in C#/.NET 4.5 and I have a dictionary defined like this:

Dictionary<int, string> d = new Dictionary<int, string>();

My integer keys are sequential and unique and, except for empty strings, my values will be unique as well. For example:

Key     Value
  0     AAAAAAAAAA
  1     BBBBBB
  2     (empty string)
  3     CCCCCCCCCCCCCCCCCCCCCCCC
  4     DDDDDDDDDDDDDDD
  5     EEEEEEEEEEEEEEEEEEE
  6     (empty string)

When I have a new string to add to the dictionary, I need to assign it to one of the keys which has a value of (empty string). Ideally, I'd like to do this at the first instance of such a key. I'm currently looping through my dictionary like this:

int keyNumber;

foreach (KeyValuePair<int, string> pair in d)
{
    if (pair.Value == string.Empty)
    {
        keyNumber = pair.Key;
        break;
    }
}

d[keyNumber] = "new string goes here!";

This works but is there a better (or faster) way to accomplish the same thing? If this actually is the best approach, is it possible to shorten it into a LINQ expression?

EDIT -----------------------------------------------------------------------------

I left out what was probably something important to explain. The values that get loaded into my dictionary are actually loaded from a binary file comprised of fixed-length byte arrays which hold ASCII strings. The file will always consist of 5000000 bytes or 100000 total strings @ 50 bytes each. The file structure was determined by some hardware limitations on the device that will consume the file. So the key for each KeyValuePair in my dictionary is actually used to determine the offset into the file where I need to write any changes when the user is finished making them. For example, using a a key of 4, I then multiply by 50 to a starting offset of 200 bytes. Then I write the modified string back at that location. Sorry for omitting that in my original post.

Upvotes: 3

Views: 9423

Answers (4)

Peter Rasmussen
Peter Rasmussen

Reputation: 16922

I wouldn't recommend it, as the comments say, you should use a list. But here's two examples:

  Dictionary<int, string> d = new Dictionary<int, string>();
  string valueToBeInserted = "randomstring";
  var keyValuePair = d.FirstOrDefault(c => c.Value == string.Empty);
  d[keyValuePair.Key] = valueToBeInserted;

Simplified:

 Dictionary<int, string> d = new Dictionary<int, string>();
 string valueToBeInserted = "randomstring";
 d[d.FirstOrDefault(c => c.Value == string.Empty).Key] = valueToBeInserted;

Upvotes: 1

Levi Botelho
Levi Botelho

Reputation: 25214

d[d.FirstOrDefault(x => string.IsNullOrEmpty(x.Value)).Key] = "My String";

However, a dictionary is really meant to store true key-value pairs. A list sounds like it is better suited to your case where it seems like you only really need to store values. Even so, list elements are still accessible by index and you can include empty elements so there doesn't (from what you have stated) seem to be a reason to stick with a dictionary.

Upvotes: 3

Lee
Lee

Reputation: 144136

If you know there will be an entry with an empty string value you can use:

var kvp = d.First(p => p.Value == string.Empty);
d[kvp.Key] = "new string goes here!";

Upvotes: 1

Coincoin
Coincoin

Reputation: 28596

A Dictionary might not be the best choice for what you are trying to do. Try a simple List<> or an array.

A Dictionary is an optimized way to map one unique value to another non-unique one. If you are trying to do anything more than that, you are better implementing your own collection.

Upvotes: 1

Related Questions