Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

what am I doing wrong here?

>Solution :

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
   };
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading