简体   繁体   English

清单 <classtype> 使用linq在C#中进行模型更新

[英]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 现在根据条件,我需要使用linq更新ProblemViewModel的每个列表值。以下是条件

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". guidelineSectionModel.Title将包含“某些值:低强度”文本。 So i used the regx to filter the text. 所以我用了regx来过滤文本。 Is there other way i can directly check the condition in linq. 还有其他方法可以直接检查linq中的条件。 and update the model model. 并更新模型模型。

I want to update list value of ProblemViewModel model property value public bool Identified to " true " 我想更新ProblemViewModel模型属性值public bool Identified列表值public bool Identified为“ true

Currently it contain only False value. 当前,它仅包含False值。

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. 我不能放LINQ,但我认为这个答案可以解决您的目的。 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: 如果您更喜欢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. 注意:由于Regex.Matches可能存在性能问题,请阅读此答案https://stackoverflow.com/a/2962689/1525637,而应使用String.Contains

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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