I'm attempting to make the code below run as quickly as possible. It takes about couple mins to get the results added the finaloutput.
This code is part of API endpoint that get vehicle information, foreach loops over each vehicle to count the number of plates from PlateVehicle table and from another policies table to get insurance use.
foreach(var vehicle in result)
{
//Get total Plates Count
var plates = await _context.PlateVehicles
.Where(a => a.VehicleId.Equals(vehicle.VehicleId)
&& a.Status=="Active").ToListAsync();
vehicle.PlatesCount = plates.Count();
//Get Insurance Use from Policy
var policyInfo = await _context.Policies
.Where(a=>a.Vehicle_Id.Equals(vehicle.VehicleId)).FirstOrDefaultAsync();
if (policyInfo != null)
{
vehicle.InsuranceUse = policyInfo.Class == null ? string.Empty : policyInfo.Class;
finalOutput.Add(vehicle);
}
}
there are around 6000 vehicle records in the DB and it is currently taking about 2 mins to get the results. Any suggestions greatly appreciated. Thanks.