0

I have some ViewModel like this.

public class ModelResult
{
   public string NAME { get; set; }
   public List<DataPointVM> RESULTs { get; set; }
}
public class DataPointVM
{
   private float v1;
   private float v2;
   public DataPointVM(float v1, float v2)
   {
      this.TIME = v1;
      this.VALUE = v2;
   }

   public float TIME { get; set; }
   public float VALUE { get; set; }
}

Then, I have a list key

List<string> parameterList = new List<string>(new string[] {
   "Jhon",
   "Andy",
   "Ivan",
   "Erick",
});

DataPoint ViewModel will store data like [{"Car1":"Bentley"},{"Car2":"Ferarri"}]

I try with this code but the result is only display last key (Erick) example:[{"NAME":"ERICK","RESULTs":[{"car1":"xxx"},{"car2":"yyy"}]}]

ModelResult mdlres = new ModelResult();
            for (index=0;index<parameterList.Count();index++)
            {
                List<DataPointVM> points = new List<DataPointVM>();
                    mdlres.NAME = parameterList[index];
                    for (int i = 0; i < 2; i++)
                    {
                        points.Add(new ViewModels.DataPointVM(tVal[i], rVal[i]));
                    }
                    mdlres.RESULTs = points.ToList();
            }
            return mdlres;

I want to get all listkey with the details.

  • Where did tVal and rVal come from? Do you have multiple copies of those for the different names? – TheSoftwareJedi Oct 09 '18 at 13:20
  • 2
    `mdlres.RESULTs = points.ToList();` should be `mdlres.RESULTs.AddRange(points);` – Zohar Peled Oct 09 '18 at 13:20
  • Possible duplicate of [Joining two lists together](https://stackoverflow.com/questions/1528171/joining-two-lists-together) – Dmitry Pavliv Oct 09 '18 at 13:30
  • Your `ModelResult` only has a single name in it. Do you want multiple names in there? Does each name need its own set of data points? You either need to return a List, or add another layer here. – TheSoftwareJedi Oct 09 '18 at 13:46
  • Sorry my fault, I have write the solution here. https://pastebin.com/uB2n6X3C I should put ```ModelResult mdlres = new ModelResult();``` inside first loop and ```newList.mresult.Add(mdlres);``` – Konsultan IT Bandung Oct 09 '18 at 13:46
  • @HendraSYP if you solved your own problem, can you post it as an answer for others in the future? – TheSoftwareJedi Oct 09 '18 at 14:12

1 Answers1

0

I'm sorry for my thread, but I've solve my problem.

ModelResult mdlres = new ModelResult(); 
// insert detail into detail list 
// exit loop
newList.mresult.Add(mdlres); create collection of mdlres

My code have been published here: https://pastebin.com/uB2n6X3C