C# - Adding data to list inside list

前端 未结 5 786
耶瑟儿~
耶瑟儿~ 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:05

    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

提交回复
热议问题