1

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?

6
  • 1
    Can you edit your snippet to be a minimal reproducible example? Commented Apr 5, 2022 at 9:27
  • 1
    Looks like your creating an object each time you do the loop overriding the values each time Commented Apr 5, 2022 at 9:29
  • @MarkDavies yes, I think this is the issue, however how should I add to the existing array? Commented Apr 5, 2022 at 9:30
  • What array? and why not use a List? Commented Apr 5, 2022 at 9:30
  • Does this answer your question? Copy Arrays to Array Commented Apr 5, 2022 at 9:30

1 Answer 1

4

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
   };
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.