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
}
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)
None
0 Points
1 Post
Construct a list of object and grouped objects
Jul 15, 2019 04:29 PM|Emwat|LINK
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?
Participant
1660 Points
952 Posts
Re: Construct a list of object and grouped objects
Jul 15, 2019 10:56 PM|PaulTheSmith|LINK
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)
Contributor
3140 Points
983 Posts
Re: Construct a list of object and grouped objects
Jul 16, 2019 05:12 AM|Yang Shen|LINK
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:
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