简体   繁体   中英

list<classtype> model update in C# using linq

I have a model structure as illustrate below.

 public class GuideLineSectionsViewModel 
    {

        public GuideLineSectionsViewModel()
        {
            SectionsSet = new List<SectionViewModel>();
        }
         public string Title { get; set; }
        public List<SectionViewModel> SectionsSet { get; set; }
    }



    public class SectionViewModel
    {

        public SectionViewModel()
        {
            SectionsSet = new List<SectionViewModel>();
            QuestionsSet = new List<QuestionViewModel>();
            ProblemsSet = new List<ProblemViewModel>();
            GoalsSet = new List<GoalViewModel>();
            BarriersSet = new List<BarriersViewModel>();
            QuestionReferencesSet = new List<QuestionReferenceViewModel>();
        }

        public string Heading { get; set; }
        public List<SectionViewModel> SectionsSet { get; set; }
        public List<QuestionViewModel> QuestionsSet { get; set; }
        public List<ProblemViewModel> ProblemsSet { get; set; }
        public List<GoalViewModel> GoalsSet { get; set; }
        public List<BarriersViewModel> BarriersSet { get; set; }
        public List<QuestionReferenceViewModel> QuestionReferencesSet { get; set; }
    }



    public class ProblemViewModel 
    {
        public string Text { get; set; }
        public bool Identified { get; set; }
        public List<GoalViewModel> GoalsSet { get; set; }
        public List<QuestionReferenceViewModel> QuestionReferencesSet { get; set; }
    }

Now Based on the condition I need to update the every list value of the ProblemViewModel using linq.Below is the condition

public GuideLineSectionsViewModel FindGuidelineType(GuideLineSectionsViewModel guidelineSectionModel)
            {
                //GuideLineSectionsViewModel result = new GuideLineSectionsViewModel();
                string title = guidelineSectionModel.Title;
                int count = Regex.Matches(title, "Low Intensity").Count;
                if (count > 0)
                {

                }
                return guidelineSectionModel; 
            }

The guidelineSectionModel.Title will contain the text as "some value : Low Intensity". So i used the regx to filter the text. Is there other way i can directly check the condition in linq. and update the model model.

I want to update list value of ProblemViewModel model property value public bool Identified to " true "

Currently it contain only False value.

Please can anyone help me to solve the issue.

Have a look at following method. I could not put LINQ but I think this answer can solve your purpose. Again Some classes structure are missing in your question so you may need to put that in following method.

GuideLineSectionsViewModel FindGuidelineType(GuideLineSectionsViewModel guidelineSectionModel)
            {
                //GuideLineSectionsViewModel result = new GuideLineSectionsViewModel();
                string title = guidelineSectionModel.Title;
                int count = Regex.Matches(title, "Low Intensity").Count;
                if (count > 0)
                {
                   foreach(SectionViewModel svm in guidelineSectionModel.SectionsSet)
                   {
                      foreach(ProblemViewModel pvm in svm.ProblemsSet)
                      {
                         pvm.Identified = true;
                      }
                    }
                }
                return guidelineSectionModel; 
            }

If you prefer LINQ:

if(guideLine.Title.Contains("Low Intensity"))
{
    guideLine.SectionsSet.ForEach(s => s.ProblemsSet.ForEach(ps => ps.Identified = true));
}

Note: please read this answer https://stackoverflow.com/a/2962689/1525637 due to possible performance problems with the Regex.Matches , you should use String.Contains instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM