I can’t seem to load an “entity” into a dictionary. I get a “Object reference not set to an instance of an object” when trying to add anything to the dictionary. Any help is greatly appreciated.
private TranslationEntities _db = new TranslationEntities();
private Dictionary<int, Language> _data;
private void LoadData()
{
var languages = _db.Languages.Include("Region").OrderBy(e => e.Region.Name).ThenBy(e => e.Name);
foreach (Language item in languages)
{
_data.Add(item.Id, item); //// ERRORS HERE ////
}
}
Initialize your
dictionarybefore invoking any functions on that.Here i initialized inside the method. You may consider initializing the dictionary in your class
constructoras well depending on your scenario/code.