Skip to content

Conversation

fiseni
Copy link
Collaborator

@fiseni fiseni commented Jun 9, 2025

Added AsList extension. If the IEnumerable source is a List, we return the list, otherwise, we create a new one.

The PostProcessingAction is a Func<IEnumerable<T>, IEnumerable<T>> delegate. However, users do not always filter the collection; they may just iterate over it and update items. In that case, if the delegate output is List, we may directly return it instead of allocating a new one.

@fiseni fiseni requested a review from Copilot June 9, 2025 09:39
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces an AsList extension to avoid unnecessary list allocations when PostProcessingAction returns an existing List<T>, updates null checks to use is null, and applies AsList across various evaluators and repository methods.

  • Added CollectionExtensions.AsList to return the source list if already a List<T>, otherwise call ToList().
  • Replaced == null with is null for PostProcessingAction checks.
  • Updated EF Core and EF6 ListAsync and DbSetExtensions methods to use AsList instead of always calling ToList().

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
InMemorySpecificationEvaluator.cs Switched null checks to is null for PostProcessingAction.
CollectionExtensions.cs Added AsList extension to reuse existing List<T>.
RepositoryBaseOfT.cs (EF Core) Updated to use AsList after post-processing instead of ToList().
DbSetExtensions.cs (EF Core) Updated to use AsList when applying PostProcessingAction.
ContextFactoryRepositoryBaseOfT.cs Updated to use AsList after post-processing in factory repo.
RepositoryBaseOfT.cs (EF6) Updated to use AsList after post-processing instead of ToList().
DbSetExtensions.cs (EF6) Updated to use AsList when applying PostProcessingAction.
Comments suppressed due to low confidence (3)

src/Ardalis.Specification/CollectionExtensions.cs:5

  • [nitpick] Add an XML doc comment for AsList<T> to explain its behavior and intent for future maintainers.
public static List<T> AsList<T>(this IEnumerable<T> source)

src/Ardalis.Specification/CollectionExtensions.cs:7

  • Add unit tests for AsList to cover both scenarios: when source is already a List<T> and when it is not, verifying that the returned instance and contents are correct.
if (source is List<T> list)

src/Ardalis.Specification/CollectionExtensions.cs:12

  • The call to ToList() requires the System.Linq namespace; add using System.Linq; at the top of this file to ensure it compiles.
return source.ToList();

@fiseni fiseni merged commit 5285862 into main Jun 9, 2025
1 check passed
@fiseni fiseni deleted the fiseni/aslist-extension branch June 9, 2025 09:49
@fiseni fiseni mentioned this pull request Jul 17, 2025
21 tasks
This was referenced Aug 20, 2025
This was referenced Oct 1, 2025
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.

1 participant