繁体   English   中英

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

[英]list<classtype> model update in C# using linq

我有一个模型结构,如下所示。

 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; }
    }

现在根据条件,我需要使用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; 
            }

guidelineSectionModel.Title将包含“某些值:低强度”文本。 所以我用了regx来过滤文本。 还有其他方法可以直接检查linq中的条件。 并更新模型模型。

我想更新ProblemViewModel模型属性值public bool Identified列表值public bool Identified为“ true

当前,它仅包含False值。

请任何人帮我解决问题。

看看下面的方法。 我不能放LINQ,但我认为这个答案可以解决您的目的。 同样,您的问题中缺少某些类的结构,因此您可能需要将其放入以下方法中。

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; 
            }

如果您更喜欢LINQ:

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

注意:由于Regex.Matches可能存在性能问题,请阅读此答案https://stackoverflow.com/a/2962689/1525637,而应使用String.Contains

暂无
暂无

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

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