'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?
Solution 1:[1]
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
};
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
