Home > Software engineering >  How to insert data into Dictionary<Model, List<Model>() using C#
How to insert data into Dictionary<Model, List<Model>() using C#

Time:01-10

I need to insert few dummy data into the Dictionary, Below is what I have tried.

            var fakesites = new List<Site> { new Site { Id = 1 } };
            var fakedata = new Dictionary<Gateway, List<FeMeasurementValues>>()
            {
                    new Gateway { SiteId = 1, FirmwareVersion = "1.1.1", ConnectivityStatus = GatewayConnectivityStatus.ReadyToConnect },
                    new Gateway { SiteId = 1, FirmwareVersion = "1.1.3", ConnectivityStatus = GatewayConnectivityStatus.Connected },
            };


            GenerateLoRaSignalAnalysisReport(fakesites, fakedata, DateTime.Now);

I get the error saying "There is no argument given that corresponds to the required formal parameter 'value' of Dictionary<Gateway, List>.Add(Gateway, List)"

Please help me.

CodePudding user response:

In C#, to add an element to a dictionary, you need to provide both the key and the value. The key is the first element in the parentheses, and the value is the second element.

In the code you posted, it looks like you are trying to add two Gateway objects as keys to the fakedata dictionary, but you are not providing any values for these keys. This is causing the error message "There is no argument given that corresponds to the required formal parameter 'value' of Dictionary<Gateway, List>.Add(Gateway, List)" to be thrown.

To fix the error, you need to provide a value for each key you are adding to the dictionary. For example:

var fakedata = new Dictionary<Gateway, List<FeMeasurementValues>>()
{
    {
        new Gateway { SiteId = 1, FirmwareVersion = "1.1.1", ConnectivityStatus = GatewayConnectivityStatus.ReadyToConnect },
        new List<FeMeasurementValues>()
    },
    {
        new Gateway { SiteId = 1, FirmwareVersion = "1.1.3", ConnectivityStatus = GatewayConnectivityStatus.Connected },
        new List<FeMeasurementValues>()
    }
};

Hope it helps !

  • Related