Phill Sanders
Phill Sanders

Reputation: 487

StringBuilder within IEnumerable

I have a ControlMeasure table that holds information on each control measure and a ControlMeasurepeopleExposed Table that holds a record for each person exposed in the control measure this could be 1 record or many records.

I Have a controller that populates a List view

For each item in the list, Control Measure, I would like to create a string that shows all the People at risk

e.g.

PeopleString = "Employees, Public, Others";

Ive added a foreach in the controller to show what I'm trying to do however I'm aware that this wont work.

The controller is this:

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

I know I cant directly put this code in the controller but it does represent what I want to do.

Upvotes: 0

Views: 1073

Answers (1)

Dave Zych
Dave Zych

Reputation: 21887

You can't foreach within an object initializer (which is what you're trying to do when instantiating FullControlMeasureListViewModel). You can, however, use a combination of string.Join and peopleExp.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
    });

Upvotes: 2

Related Questions