C# - Adding data to list inside list

前端 未结 5 822
耶瑟儿~
耶瑟儿~ 2021-01-14 00:40

How can I add the following data on the table into a list called Vehicles?

public class criterias
{
    public double values { get; set; }
    publi         


        
5条回答
  •  日久生厌
    2021-01-14 01:03

    it seems in your table the VehicleId is of type string. Make sure your VehicleId property in Vehicle class also matches the same.

    You can use the collection initializers to set the values of child objects like this way:

     var data = new vehicles()
            {
                vehID = 1,
                vehDescription = "Average Car",
                vehValCriteria = new List()
                {
                    new stepsList()
                    {
                        steps = "Move car",
                        stepChannelsCriteria = new List()
                        {
                            new movChannels()
                            {
                                name = "engage firstgear",
                                criteria = new List()
                                {
                                    new criterias()
                                    {
                                        values = 1,
                                        time = 1
                                    },
                                }
                            },
                            new movChannels()
                            {
                                name = "reach 10kph",
                                criteria = new List()
                                {
                                    new criterias()
                                    {
                                        values = 10,
                                        time = 5
                                    },
                                }
                            },
                            new movChannels()
                            {
                                name = "maintain 10kph",
                                criteria = new List()
                                {
                                    new criterias()
                                    {
                                        values = 10,
                                        time = 12
                                    },
                                }
                            }
                        }
                    },
                    new stepsList()
                    {
                        steps = "stop car",
                        stepChannelsCriteria = new List()
                        {
                            new movChannels()
                            {
                                name = "reach okph",
                                criteria = new List()
                                {
                                    new criterias()
                                    {
                                        values = 10,
                                        time = 4
                                    },
                                }
                            },
                            new movChannels()
                            {
                                name = "put in neutral",
                                criteria = new List()
                                {
                                    new criterias()
                                    {
                                        values = 0,
                                        time = 1
                                    },
                                }
                            },
                            new movChannels()
                            {
                                name = "turn off vehicle",
                                criteria = new List()
                                {
                                    new criterias()
                                    {
                                        values = 0,
                                        time = 0
                                    },
                                }
                            }
                        }
                    }
                }
            };
    

提交回复
热议问题