Baldie47
Baldie47

Reputation: 1254

Adding an array property into an array of another object

I have this call that returns an array of treatments

 var procedures = await client.GetProceduresAsync(clinicId);

I was trying to loop and insert all procedureIds (from the array) into an array property of the availableSlotsQuery

var availableSlotsQuery = new AvailableSlotsQuery();
            foreach (var procedure in procedures.Select(x=> x.Procedure))
            {
                availableSlotsQuery = new AvailableSlotsQuery
                {
                    ClinicId = clinicId,
                    ProcedureIds = new [] { procedure.Id},
                    Start = request.From.ToDateTimeOffset(),
                    End = request.To.ToDateTimeOffset(),
                    CaregiverId = therapistId?.Id
                };
            }

This is not working.

ProcedureIds is a string [] but after looping I only have one id in the ProcedureIds property

what am I doing wrong here?

Upvotes: 1

Views: 71

Answers (1)

Gabriel Llorico
Gabriel Llorico

Reputation: 1803

with looping

var availableSlotsQuery = new AvailableSlotsQuery();
availableSlotsQuery = new AvailableSlotsQuery
{
    ClinicId = clinicId,
    Start = request.From.ToDateTimeOffset(),
    End = request.To.ToDateTimeOffset(),
    CaregiverId = therapistId?.Id
};

var listOfProcedureIds = new List<string>();
foreach (var procedure in procedures.Select(x=> x.Procedure))
{
    listOfProcedureIds.Add(procedure.Id);
}

availableSlotsQuery.ProcedureIds = listOfProcedureIds.ToArray();

without looping

availableSlotsQuery = new AvailableSlotsQuery
{
    ClinicId = clinicId,
    Start = request.From.ToDateTimeOffset(),
    End = request.To.ToDateTimeOffset(),
    CaregiverId = therapistId?.Id,
    ProcedureIds = procedures.Select(x => x.Procedure.Id).ToArray()
};

as mentioned by all, you are creating a new object in your foreach statement

foreach (var procedure in procedures.Select(x=> x.Procedure))
{
//as you can see here with the availableSlotQuery = new AvailableSlotQuery
   availableSlotsQuery = new AvailableSlotsQuery
   {
   //properties
   };
}

Upvotes: 4

Related Questions