6

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

enter image description here

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

public class movChannels
{
    public string name { get; set; }
    public IList<criterias> criteria = new List<criterias>();
}

public class stepsList
{
    public string steps { get; set; }
    public IList<movChannels> stepChannelsCriteria = new List<movChannels>();
}

public class vehicles
{
    public int vehID { get; set; }
    public string vehDescription { get; set; }
    public IList<stepsList> vehValCriteria = new List<stepsList>();
}

Now, how can I add the data that I have in the table shown into a list called Vehicles? I will create other vehicles later...

1
  • 1
    Start with initializing vehicles instance and setting its vehID and vehDescription. Then create stepsList instance and add it to vehValCriteria List and etc. Commented May 17, 2016 at 8:57

5 Answers 5

13

You had several bad decisions, some were design flaws and some were minor C# naming convention violations.

Couple of worth mentions flaws:

  1. vehID should have been a string and not int (Example "XPT")
  2. Movment has Name, Value and Time. It doesn't have a list of Values and Times.

Creation:

List<Vehicle> vehicles = new List<Vehicle>();

Vehicle vehicle = new Vehicle()
{
    Id = "XPT",
    Description = "Average Car",
    Steps = new List<Step>()
    {
        new Step() {
            Name = "move car",
            Movements = new List<Movement>()
            {
                new Movement("engage 1st gear", 1, 1),
                new Movement("reach 10kph", 10, 5),
                new Movement("maintain 10kph", 10, 12),
            }
        },
        new Step() {
            Name = "stop car",
            Movements = new List<Movement>()
            {
                new Movement("reach 0kph", 10, 4),
                new Movement("put in neutral", 0, 1),
                new Movement("turn off vehicle", 0, 0),
            }
        }
    }
};
vehicles.Add(vehicle);

Entities:

public class Movement
{
    public string Name { get; set; }
    public double Values { get; private set; }
    public double Time { get; private set; }

    public Movement(string name, double values, double time)
    {
        Name = name;
        Values = values;
        Time = time;
    }
}

public class Step
{
    public string Name { get; set; }
    public IList<Movement> Movements { get; set; }
}

public class Vehicle
{
    public string Id { get; set; } // Should be changed to string
    public string Description { get; set; }
    public IList<Step> Steps { get; set; }
}
Sign up to request clarification or add additional context in comments.

7 Comments

cannot use the type var I don't know why
@peetman, code updated. (Also added vehicles.Add(...);) at the end.
thanks. if I have more than one criteria to each Movement should I just replace the Values and Time with another class as I suggested in the Question?
@peetman, Indeed, Good job. If you still can't make that happend, tell me and i will refactor. Feel free to upvote and accept if the post answered your question.
Thanks. How can access the created list in a another class(.cs)?
|
1

You should create your classes like the following:

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

public class movChannels
{
    public movChannels
    {
        criteria = new List<criterias>();
    }
    public string name { get; set; }
    public IList<criterias> criteria { get; set; }
}

public class stepsList
{
    public stepsList
    {
        stepChannelsCriteria = new List<movChannels>();
    }
    public string steps { get; set; }
    public IList<movChannels> stepChannelsCriteria { get; set; }
}

public class vehicles
{
    public vehicles
    {
        vehValCriteria = new List<stepsList>();
    }
    public int vehID { get; set; }
    public string vehDescription { get; set; }
    public IList<stepsList> vehValCriteria { get; set; }
    public movChannels movments { get; set; }
}

Comments

0

What about that?

public class VehiclesViewModel
{
    public List<vehicles> Vehicles { get; private set; }

    public void Initalize()
    {
      this.Vehicles  = new List<vehicles>();

        var vehicle = new vehicles
                       {
                           vehID = 1,
                           vehDescription = "firstDescription",
                       };
        var stepsList = new stepsList
                        {
                            steps = "firstStep",
                        };
        var movChannel = new movChannels
                          {
                              name = "firstChannel",
                          };
        var criteria = new criterias
                        {
                            values = 0.5,
                            time = 0.5
                        };

        movChannel.criteria.Add(criteria);
        stepsList.stepChannelsCriteria.Add(movChannel);
        vehicle.vehValCriteria.Add(stepsList);

        this.Vehicles.Add(vehicle);
    }
}

Comments

0

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<stepsList>()
            {
                new stepsList()
                {
                    steps = "Move car",
                    stepChannelsCriteria = new List<movChannels>()
                    {
                        new movChannels()
                        {
                            name = "engage firstgear",
                            criteria = new List<criterias>()
                            {
                                new criterias()
                                {
                                    values = 1,
                                    time = 1
                                },
                            }
                        },
                        new movChannels()
                        {
                            name = "reach 10kph",
                            criteria = new List<criterias>()
                            {
                                new criterias()
                                {
                                    values = 10,
                                    time = 5
                                },
                            }
                        },
                        new movChannels()
                        {
                            name = "maintain 10kph",
                            criteria = new List<criterias>()
                            {
                                new criterias()
                                {
                                    values = 10,
                                    time = 12
                                },
                            }
                        }
                    }
                },
                new stepsList()
                {
                    steps = "stop car",
                    stepChannelsCriteria = new List<movChannels>()
                    {
                        new movChannels()
                        {
                            name = "reach okph",
                            criteria = new List<criterias>()
                            {
                                new criterias()
                                {
                                    values = 10,
                                    time = 4
                                },
                            }
                        },
                        new movChannels()
                        {
                            name = "put in neutral",
                            criteria = new List<criterias>()
                            {
                                new criterias()
                                {
                                    values = 0,
                                    time = 1
                                },
                            }
                        },
                        new movChannels()
                        {
                            name = "turn off vehicle",
                            criteria = new List<criterias>()
                            {
                                new criterias()
                                {
                                    values = 0,
                                    time = 0
                                },
                            }
                        }
                    }
                }
            }
        };

Comments

0

You can fill your list by moving from top to bottom, like

  • Create Criterias List then Create movChannel object and add that list to Criterias object and so on

However if you want to avoid this way, there is another way. If you are using Linq To List then follow this

Get a simple flat object to a list object

var TableData = db.Tablename.Tolist(); 

Then fill your own object like this

Vehicles finalList = TableData.Select(a => new Vehicles()
{
    vehID = a.Id,
    vehDescription = a.des,
    vehValCriteria = TableData.Where(b => b.StepslistId == a.StepslistId)
    .Select(c => new StepsList()
    {
                steps = c.Steps,
                stepChannelsCriteria = TableData.Where(d => d.channelId == c.channelId)
                .select(e => new MovChannels()
                {
                    name = e.name,
                    criteria = TableData.Where(f => f.criteriasId = e.criteriasId)
                    .Select(g => new Criterias()
                    {
                        values = g.Values,
                        time = g.Time

                    }).ToList()
                }).ToList()
         }).ToList()
    }).ToList();

This is standard way to fill list within list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.