Add gps coordinates to polling stations#1048
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| foreach (var groupedPollingStationId in groupedPollingStationIds) | ||
| { | ||
| if (groupedPollingStationId.numberOfDuplicates > 1) | ||
| { | ||
| AddError(new ValidationFailure(groupedPollingStationId.id.ToString(), "This id is duplicated")); | ||
| } | ||
| } |
Check notice
Code scanning / CodeQL
Missed opportunity to use Where Note
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 11 days ago
In general, to fix this kind of issue you replace a foreach loop that uses an if guard (or continue) to filter items with a foreach over a filtered sequence using .Where(...). This makes the filtering explicit at the sequence level instead of inside the loop body.
For this specific code in api/src/Feature.PollingStations/Create/Endpoint.cs, the best fix is to add a .Where clause to groupedPollingStationIds so that the loop iterates only over those groups where numberOfDuplicates > 1. Concretely, change:
foreach (var groupedPollingStationId in groupedPollingStationIds)
{
if (groupedPollingStationId.numberOfDuplicates > 1)
{
AddError(new ValidationFailure(groupedPollingStationId.id.ToString(), "This id is duplicated"));
}
}to:
foreach (var groupedPollingStationId in groupedPollingStationIds.Where(g => g.numberOfDuplicates > 1))
{
AddError(new ValidationFailure(groupedPollingStationId.id.ToString(), "This id is duplicated"));
}This removes the inner if and expresses the same condition via Where. No additional imports are necessary because LINQ extension methods like Where are already being used earlier in the file (req.PollingStations.Where(...)), implying the relevant using System.Linq; is present somewhere in the compilation unit. Only the loop region (lines 41–47) needs to be updated.
| @@ -38,12 +38,9 @@ | ||
| var groupedPollingStationIds = pollingStationIds.GroupBy(id => id, y => y, | ||
| (id, duplicates) => new { id, numberOfDuplicates = duplicates.Count() }); | ||
|
|
||
| foreach (var groupedPollingStationId in groupedPollingStationIds) | ||
| foreach (var groupedPollingStationId in groupedPollingStationIds.Where(g => g.numberOfDuplicates > 1)) | ||
| { | ||
| if (groupedPollingStationId.numberOfDuplicates > 1) | ||
| { | ||
| AddError(new ValidationFailure(groupedPollingStationId.id.ToString(), "This id is duplicated")); | ||
| } | ||
| AddError(new ValidationFailure(groupedPollingStationId.id.ToString(), "This id is duplicated")); | ||
| } | ||
|
|
||
| ThrowIfAnyErrors(); |
No description provided.