Skip to content

CodeQL 14: refactor: convert foreach-add patterns to LINQ Select#200

Open
rlorenzo wants to merge 3 commits into
mainfrom
codeql/14-effort-pdf-select
Open

CodeQL 14: refactor: convert foreach-add patterns to LINQ Select#200
rlorenzo wants to merge 3 commits into
mainfrom
codeql/14-effort-pdf-select

Conversation

@rlorenzo
Copy link
Copy Markdown
Contributor

@rlorenzo rlorenzo commented May 13, 2026

Summary

Closes 3 of the 28 cs/linq/missed-select alerts where the foreach body is a simple build-and-add into a context/list:

  • CourseController.AddSessionCompetency (line 227) - foreach (var levelId in LevelIds) { var x = new SessionCompetency(...); context.Add(x); }context.AddRange(LevelIds.Select(levelId => new SessionCompetency(...))).
  • CourseController.UpdateSessionCompetency (line 273, same pattern).
  • IndividualSearchResult (line 130) - foreach (var r in results) { EmailHost = r.Split("@")[^1]; }Select(...) to materialize the list, then guard on count before assigning the last entry. Semantically identical.

Not addressed (still open)

The remaining 25 cs/linq/missed-select alerts are all in PDF/Excel cell-generation loops in Effort report services where the foreach body calls side-effectful QuestPDF.table.Cell()…Text(…) or ClosedXML.ws.Cell()…Value. Forcing those into Select just renames a single local without removing the iteration or its side effects, so the resulting code is the same length with worse readability. Those should be dismissed at the dashboard.

Context

Fourteenth in the CodeQL N: cleanup series.

Test plan

  • npm run test:backend - 1946 tests passing
  • Pre-commit lint+test+verify all passed

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 13, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: f558cfdf-1072-4242-b853-4a52cb14714e

📥 Commits

Reviewing files that changed from the base of the PR and between 5949e52 and dfdad52.

📒 Files selected for processing (2)
  • web/Areas/CTS/Controllers/CourseController.cs
  • web/Areas/Directory/Models/IndividualSearchResult.cs

📝 Walkthrough

Walkthrough

Two refactorings replace iteration patterns with LINQ batch and streaming operations. CourseController batch-inserts SessionCompetency entities using AddRange with projections; IndividualSearchResult streams email domain extraction into a materialized list instead of eager loading.

Changes

Batch operations and streaming refactors

Layer / File(s) Summary
CourseController batch insertion for session competencies
web/Areas/CTS/Controllers/CourseController.cs
AddSessionCompetency and UpdateSessionCompetency replace per-item context.Add() calls inside foreach loops with single context.AddRange() calls projecting SessionCompetency entities from LevelIds and toAdd collections, preserving field mappings for competency, session, level, role, and order defaults.
IndividualSearchResult email host extraction pipeline
web/Areas/Directory/Models/IndividualSearchResult.cs
LookupEmailHost replaces eager ToList() followed by per-item domain splitting with a LINQ pipeline that streams SqlQueryRaw results, selects the host portion from each email, materializes into a list, and sets EmailHost from the last extracted host when non-empty.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main refactoring work: converting foreach-add patterns to LINQ Select, which is the primary change across both modified files.
Description check ✅ Passed The description is directly related to the changeset, clearly explaining the three specific conversions made and the rationale for not addressing the remaining alerts.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codeql/14-effort-pdf-select

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov-commenter
Copy link
Copy Markdown

Bundle Report

Bundle size has no change ✅

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 13, 2026

Codecov Report

❌ Patch coverage is 0% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 42.96%. Comparing base (5949e52) to head (dfdad52).

Files with missing lines Patch % Lines
web/Areas/CTS/Controllers/CourseController.cs 0.00% 15 Missing ⚠️
...b/Areas/Directory/Models/IndividualSearchResult.cs 0.00% 6 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #200   +/-   ##
=======================================
  Coverage   42.96%   42.96%           
=======================================
  Files         877      877           
  Lines       51468    51462    -6     
  Branches     4802     4800    -2     
=======================================
  Hits        22113    22113           
+ Misses      28831    28825    -6     
  Partials      524      524           
Flag Coverage Δ
backend 43.04% <0.00%> (+<0.01%) ⬆️
frontend 41.34% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

S2971: SqlQueryRaw().ToList().Select(...) materializes the entire
result set into a List just to throw it away. Replace with
AsEnumerable() — same effect of switching to LINQ-to-objects so the
Split isn't translated to SQL, without the extra allocation.
@rlorenzo
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@rlorenzo
Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 19, 2026

✅ Actions performed

Full review triggered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants