0

I want to set null into key that defined int?:

My code:

Dictionary<int?, string> dic=> Config.Where(a => a.Field == nameof(SafeFilesPath)).OrderBy(s => s.IndexOrder).ToDictionary(a => a.IndexOrder, b => b.Value);

IndexOreder can be also null.

And it fails. I must this null-key. What can I do???

I select all the parameters from DataBase. And the Keys can be also NULL.

Ayal
  • 133
  • 1
  • 1
  • 11
  • What´s the point of having Dictionary with null keys?. Wouldn't that be a List? – NicoRiff Jan 02 '19 at 13:30
  • @NicoRiff: Because there are times there is a key and there are times when I want to have one key with `NULL` – Ayal Jan 02 '19 at 13:32
  • @Ayal if the data comes from a database, `NULL` means there's no value, not a `NULL` value. You can't use `Where someField = NULL` in a database either. I'd question a database design where a key can be null in the first place. If you can't improve the query that loads those values, a good solution would be to use a magic value as vc74 suggested. I'd suggest `int.MinValue` though as database keys are typically positive numbers – Panagiotis Kanavos Jan 02 '19 at 13:49
  • 1
    @Ayal if you want to store unknown/missing/not-applicable entries in the database (eg in a lookup table or dimension table), use well-known values like -1 for `Missing`, -2 for `Not Applicable` etc – Panagiotis Kanavos Jan 02 '19 at 13:50
  • There is no option to put a NULL value ??? I want to put the values as they are! No magic values – Ayal Jan 02 '19 at 13:52
  • The only thing you could do is wrap your `int?` into a custom class which stores the `int?` so something like `Dictionary dic=> Config….ToDictionary(a => new MagicInt(a.IndexOrder), b => b.Value);` your custom class Needs to override Equals and GetHashCode. – Rand Random Jan 02 '19 at 14:19
  • Another idea - you could cast your `int` into `double` and use `double.NaN` as the null indicator. Like this: `Dictionary dic=> Config….ToDictionary(a => (double?)a.IndexOrder ?? double.NaN, b => b.Value);` – Rand Random Jan 02 '19 at 14:23

1 Answers1

0

As long as an object is used as a key in the Dictionary, it must not change in any way that affects its hash value. Every key in a Dictionary must be unique according to the dictionary's equality comparer. A key cannot be null, but a value can be, if the value type TValue is a reference type.

Dictionary Class

ES2018
  • 438
  • 3
  • 15
  • But `int?` it's meen that you nullable, no? – Ayal Jan 02 '19 at 13:33
  • @Ayal no. It means you have something that can *hold* nulls, but isn't null itself. What are you trying to do in the first place? Why not use `Dictionary.TryGetValue` to check if a key exists? – Panagiotis Kanavos Jan 02 '19 at 13:36
  • Becouse I want to Insert one value with `NULL` in the key, I don't want `0` – Ayal Jan 02 '19 at 13:38
  • I select the keys from the DataBase and it's can be also `null` – Ayal Jan 02 '19 at 13:40
  • @Ayal update your question explaining what you want to do. Since you load data from the database you are probably using `ToDictionary`, not `Dictionary.Add`. Even in the database though you *can't* use a NULL as a key value, because you can't compare one null with another. `NULL` means `I don't know`. – Panagiotis Kanavos Jan 02 '19 at 13:44
  • I update the question – Ayal Jan 02 '19 at 13:47