IEnumerable中的StringBuilder

时间:2015-11-19 16:11:31

标签: c# asp.net-mvc linq

我有一个ControlMeasure表,其中包含有关每个控制措施的信息,以及一个ControlMeasurepeopleExposed表,其中包含控制措施中公开的每个人的记录,这可能是1条记录或多条记录。

我有一个填充列表视图的控制器

对于列表中的每个项目,控制措施,我想创建一个字符串,显示所有有风险的人

e.g。

PeopleString = "Employees, Public, Others";

我在控制器中添加了一个foreach来显示我想要做的事情但是我知道这不会起作用。

控制器是:

        public ActionResult ControlMeasureList(int raId)
    {
        //Populate the list
        var hazards = new List<Hazard>(db.Hazards);
        var controlMeasures = new List<ControlMeasure>(db.ControlMeasures).Where(x => x.RiskAssessmentId == raId);

        var cmcombined = (
                              from g in hazards
                              join f in controlMeasures
                              on new { g.HazardId } equals new { f.HazardId }
                              select new CMCombined
                              {
                                  Activity = f.Activity,
                                  ControlMeasureId = f.ControlMeasureId,
                                  ExistingMeasure = f.ExistingMeasure,
                                  HazardName = g.Name,
                                  LikelihoodId = f.LikelihoodId,
                                  Rating = f.Rating,
                                  RiskAssessmentId = f.RiskAssessmentId,
                                  SeverityId = f.SeverityId,
                                  }).OrderBy(x => x.Activity).ToList();

        var cmPeopleExp = new List<ControlMeasurePeopleExposed>(db.ControlMeasurePeopleExposeds).Where(x => x.RiskAssessmentId == raId);
        var peopleExp = from c in cmPeopleExp
                        join d in db.PeopleExposeds
                        on c.PeopleExposedId equals d.PeopleExposedId
                        orderby d.Name
                        select new RAPeopleExp
                        {
                            RAPeopleExpId = c.PeopleExposedId,
                            PeopleExpId = c.PeopleExposedId,
                            PeopleExpName = d.Name,
                            RiskAssessmentId = c.RiskAssessmentId,
                            ControlMeasureId = c.ControlMeasureId
                        };

        var model = cmcombined.Select(t => new FullControlMeasureListViewModel
        {
            ControlMeasureId = t.ControlMeasureId,
            HazardName = t.HazardName,
            LikelihoodId = t.LikelihoodId,
            Rating = t.Rating,
            SeverityId = t.SeverityId,
            Activity = t.Activity,
            ExCM = t.ExistingMeasure,
            //This section here is where I'm struggling
            var PeopleString = new StringBuilder();
            foreach (var p in peopleExp)
            {
                PeopleString.AppendLine(p.PeopleName); 
            {
            PeopleExposed = PeopleString,
        });
        return PartialView("_ControlMeasureList", model);
    }

我知道我不能直接将此代码放在控制器中,但它确实代表了我想要做的事情。

1 个答案:

答案 0 :(得分:2)

您不能在对象初始值设定项中foreach(这是您在实例化FullControlMeasureListViewModel时尝试执行的操作)。但是,您可以使用string.JoinpeopleExp.Select的组合:

var model = cmcombined.Select(t => new FullControlMeasureListViewModel
    {
        //other props
        PeopleExposed = string.Join(",", peopleExp
                                     .Where(p => p.ControlMeasureId == t.ControlMeasureId)
                                     .Select(p => p.PeopleExpName));
        //other props
    });