locked
Construct a list of object and grouped objects RRS feed

  • Question

  • User-1293206859 posted

    I'm doing a checklist application, but it turns out my model needs to support javascript for userfriendliness, and it'll be a lot easier to work with, if the json model appropriately reflects its true intentions.

    How do I convert my list of database to do items into a more appropriate model?

            public void testChecklistItemIdea()
            {
                var viewModel = new List<MyChecklist>();
                var databaseModel = new List<MyChecklist>();
                databaseModel.Add(new MyChecklist() { CaseID = 19104, ChecklistID = 67736, TaskDescription = "First Task", TaskElement = "checkbox" });
                databaseModel.Add(new MyChecklist() { CaseID = 19104, ChecklistID = 67737, TaskDescription = "Option 1", TaskElement = "select", TemplateParentID = 2435 });
                databaseModel.Add(new MyChecklist() { CaseID = 19104, ChecklistID = 67738, TaskDescription = "Option 2", TaskElement = "select", TemplateParentID = 2435 });
                databaseModel.Add(new MyChecklist() { CaseID = 19104, ChecklistID = 67739, TaskDescription = "Option 3", TaskElement = "select", TemplateParentID = 2435 });
                databaseModel.Add(new MyChecklist() { CaseID = 19104, ChecklistID = 67740, TaskDescription = "Second Task", TaskElement = "checkbox" });
    
                //assuming the above is what I have, how do I recode the below without hard coding anything?
                viewModel.Add(databaseModel.ElementAt(0));
                foreach (var selectGroup in databaseModel.Where(w => w.TaskElement == "select" && w.TemplateParentID.HasValue).GroupBy(g => g.TemplateParentID))
                {
                    viewModel.Add(selectGroup); //cannot convert from 'System.Linq.IGrouping<int?, MyChecklist>' to MyChecklist
                }
                viewModel.Add(databaseModel.Last());
    
                return; //return viewModel... where something is like First Task, Grouped Options, Second Task
            }

    Monday, July 15, 2019 4:29 PM

All replies

  • User303363814 posted

    We can guess the definition of MyCheckList (but it would be easier if you showed us).  But what result so you want?  Given the sample input you have shown what exactly do you want as a result? You have shown something which does not compile but that doesn't define what it is you are trying to achieve!

    (What does json have to do with it?  Can't see any json in the code you have shown)

    Monday, July 15, 2019 10:56 PM
  • User-719153870 posted

    Hi Emwat,

    According to your codes, it looks like you want to store database data in the ViewModel by three times?

    Why not save all the data at once, and what are the problems you are facing now?

    If you want to convert an int? Type ‘selectGroup’ to a custom type ‘MyChecklist’, you can use (MyChecklist)selectGroup.

    Please refer to below codes:

    public void testChecklistItemIdea()
            {
                var viewModel = new List<MyChecklist>();
                var databaseModel = new List<MyChecklist>();
                databaseModel.Add(new MyChecklist() { CaseID = 19104, ChecklistID = 67736, TaskDescription = "First Task", TaskElement = "checkbox" });
                databaseModel.Add(new MyChecklist() { CaseID = 19104, ChecklistID = 67737, TaskDescription = "Option 1", TaskElement = "select", TemplateParentID = 2435 });
                databaseModel.Add(new MyChecklist() { CaseID = 19104, ChecklistID = 67738, TaskDescription = "Option 2", TaskElement = "select", TemplateParentID = 2435 });
                databaseModel.Add(new MyChecklist() { CaseID = 19104, ChecklistID = 67739, TaskDescription = "Option 3", TaskElement = "select", TemplateParentID = 2435 });
                databaseModel.Add(new MyChecklist() { CaseID = 19104, ChecklistID = 67740, TaskDescription = "Second Task", TaskElement = "checkbox" });
    
                viewModel.Add(databaseModel.ElementAt(0));
                foreach (var selectGroup in databaseModel.Where(w => w.TaskElement == "select" && w.TemplateParentID.HasValue).GroupBy(g => g.TemplateParentID))
                {
                    foreach (var select in selectGroup)
    {
    viewModel.Add((MyChecklist)select);
    } } viewModel.Add(databaseModel.Last()); return; } public class MyChecklist { public int CaseID { get; set; } public int ChecklistID { get; set; } public string TaskDescription { get; set; } public string TaskElement { get; set; } public int? TemplateParentID { get; set; } }

    Ig in this case, it's hard to understand the meaning of GroupBy(g => g.TemplateParentID). Can you describe your needs?

    Best Regard,

    Yang Shen

    Tuesday, July 16, 2019 5:12 AM