-1

i want to know how to write a better code then the below. It works, but i think there has to be a better way to write it. Maybe with some Lambda expressions ?

`

var dataList = new List<MyModel>();

        foreach (var data in dataArray)
        {
            var model = new MyModel();
            model.Name = data.Name;
            model.LastName = data.LastName;
            
            model.Model = "";
            model.SubModel = "";

            if (data.Cars.Any())
            {
                foreach (var car in data.Cars)
                {
                    model = new MyModel();
                    model.Name = data.Name;
                    model.LastName = data.LastName;
                    model.Model = car.Model;
                    model.SubModel = "";

                    if (car.SubModels.Any())
                    {
                        foreach (var subModel in car.SubModels)
                        {
                            model = new MyModel();
                            model.Name = data.Name;
                            model.LastName = data.LastName;
                            model.Model = car.Model;
                            model.SubModel = subModel.Model;
                            dataList.Add(model);
                        }
                    }
                    else
                    {
                        dataList.Add(model);
                    }
                }
            }
            else
            {
                dataList.Add(model);
            }
        }

`

Tried to convert it to Lync in VS

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
optPr1me
  • 11
  • 2
  • Ideally you should really be asking this over on [codereview.se] – phuzi Dec 23 '22 at 11:29
  • What is this code trying to do in the first place? The inner loop overwrites the objects created in the outer loop. Are you trying to flatten the hierarhcy, and emit all models along with all submodels? – Panagiotis Kanavos Dec 23 '22 at 11:37
  • Check the answers to [How to flatten tree via LINQ?](https://stackoverflow.com/questions/11830174/how-to-flatten-tree-via-linq). It's quite possible that an iterator method would be easier and *faster* than the `Flatten` methods – Panagiotis Kanavos Dec 23 '22 at 11:39
  • 1
    @phuzi the question needs work before it's suited to [codereview.se]. You should have pointed the asker at [A guide to Code Review for Stack Overflow users](//codereview.meta.stackexchange.com/a/5778), as some things are done differently over there - e.g. we need a good description of the *purpose* of the code to give context, and question titles should simply say what the code *does* (the question is always, "_How can I improve this?_"). It's important that the code works correctly; include the unit tests if possible. – Toby Speight Dec 23 '22 at 13:18

1 Answers1

0

pseudo code not tested. You need also three constructors to create the model class. Or you do it with property init:

new Model() { Name = data.Name, 
LastName = data.LastName, Model = car.Model, SubModel = subModel.Model}
        var dataList = dataArray
          .Select(item => item.Cars)
              .Select(car => new Model(data.Name,data.LastName,car.Model,subModel.Model))
          .Concat(item => item.SubModel
                    .Select(subModel => new Model(data.Name,data.LastName,car.Model,subModel.Model,subModel.Model)))
          .Concat(dataArray.Select(data => new Model(data.Name,data.LastName)))
tire0011
  • 1,048
  • 1
  • 11
  • 22