Skip to content

Commit 3d4be05

Browse files
committed
Use built-in HashSet<>.UnionWith() instead of our own .AddRange()
1 parent d5735df commit 3d4be05

File tree

11 files changed

+13
-24
lines changed

11 files changed

+13
-24
lines changed

src/JsonApiDotNetCore/CollectionExtensions.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,4 @@ public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source)
7979
return source.Where(element => element is not null)!;
8080
#pragma warning restore AV1250 // Evaluate LINQ query before returning it
8181
}
82-
83-
public static void AddRange<T>(this ICollection<T> source, IEnumerable<T> itemsToAdd)
84-
{
85-
ArgumentGuard.NotNull(source);
86-
ArgumentGuard.NotNull(itemsToAdd);
87-
88-
foreach (T item in itemsToAdd)
89-
{
90-
source.Add(item);
91-
}
92-
}
9382
}

src/JsonApiDotNetCore/Queries/Internal/Parsing/IncludeParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private ICollection<IncludeTreeNode> LookupRelationshipName(string relationshipN
110110

111111
if (relationships.Any())
112112
{
113-
relationshipsFound.AddRange(relationships);
113+
relationshipsFound.UnionWith(relationships);
114114

115115
RelationshipAttribute[] relationshipsToInclude = relationships.Where(relationship => !relationship.IsIncludeBlocked()).ToArray();
116116
ICollection<IncludeTreeNode> affectedChildren = parent.EnsureChildren(relationshipsToInclude);

src/JsonApiDotNetCore/Queries/Internal/QueryLayerComposer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ private static IImmutableSet<IncludeElementExpression> ApplyIncludeElementUpdate
254254
IDictionary<IncludeElementExpression, IImmutableSet<IncludeElementExpression>> updatesInChildren)
255255
{
256256
ImmutableHashSet<IncludeElementExpression>.Builder newElementsBuilder = ImmutableHashSet.CreateBuilder<IncludeElementExpression>();
257-
newElementsBuilder.AddRange(includeElements);
257+
newElementsBuilder.UnionWith(includeElements);
258258

259259
foreach ((IncludeElementExpression existingElement, IImmutableSet<IncludeElementExpression> updatedChildren) in updatesInChildren)
260260
{

src/JsonApiDotNetCore/Repositories/EntityFrameworkCoreRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ private IEnumerable GetRightValueToStoreForAddToToMany(TResource leftResource, H
471471

472472
if (rightResourceIdsStored.Any())
473473
{
474-
rightResourceIdsStored.AddRange(rightResourceIdsToAdd);
474+
rightResourceIdsStored.UnionWith(rightResourceIdsToAdd);
475475
return rightResourceIdsStored;
476476
}
477477

src/JsonApiDotNetCore/Resources/OperationContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ private void AddSecondaryResources(RelationshipAttribute relationship, HashSet<I
5858
object? rightValue = relationship.GetValue(Resource);
5959
IReadOnlyCollection<IIdentifiable> rightResources = CollectionConverter.ExtractResources(rightValue);
6060

61-
secondaryResources.AddRange(rightResources);
61+
secondaryResources.UnionWith(rightResources);
6262
}
6363
}

src/JsonApiDotNetCore/Resources/TargetedFields.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public void CopyFrom(ITargetedFields other)
1818
{
1919
Clear();
2020

21-
Attributes.AddRange(other.Attributes);
22-
Relationships.AddRange(other.Relationships);
21+
Attributes.UnionWith(other.Attributes);
22+
Relationships.UnionWith(other.Relationships);
2323
}
2424

2525
public void Clear()

src/JsonApiDotNetCore/Serialization/Request/Adapters/RelationshipDataAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private IEnumerable ConvertToManyRelationshipData(SingleOrManyData<ResourceIdent
111111
}
112112

113113
var resourceSet = new HashSet<IIdentifiable>(IdentifiableComparer.Instance);
114-
resourceSet.AddRange(rightResources);
114+
resourceSet.UnionWith(rightResources);
115115
return resourceSet;
116116
}
117117
}

test/JsonApiDotNetCoreTests/IntegrationTests/CustomRoutes/CustomRouteTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
6767
public async Task Can_get_resources_at_custom_action_method()
6868
{
6969
// Arrange
70-
List<Town> town = _fakers.Town.Generate(7);
70+
List<Town> towns = _fakers.Town.Generate(7);
7171

7272
await _testContext.RunOnDatabaseAsync(async dbContext =>
7373
{
7474
await dbContext.ClearTableAsync<Town>();
75-
dbContext.Towns.AddRange(town);
75+
dbContext.Towns.AddRange(towns);
7676
await dbContext.SaveChangesAsync();
7777
});
7878

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Relationships/RemoveFromToManyRelationshipTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ private void RemoveFromSubscribers(WorkItem workItem, ISet<IIdentifiable> rightR
11421142
{
11431143
if (!workItem.Subscribers.IsNullOrEmpty())
11441144
{
1145-
PreloadedSubscribers.AddRange(workItem.Subscribers);
1145+
PreloadedSubscribers.UnionWith(workItem.Subscribers);
11461146
}
11471147

11481148
foreach (long subscriberId in ExtraSubscribersIdsToRemove)
@@ -1158,7 +1158,7 @@ private void RemoveFromTags(WorkItem workItem, ISet<IIdentifiable> rightResource
11581158
{
11591159
if (!workItem.Tags.IsNullOrEmpty())
11601160
{
1161-
PreloadedTags.AddRange(workItem.Tags);
1161+
PreloadedTags.UnionWith(workItem.Tags);
11621162
}
11631163

11641164
foreach (int tagId in ExtraTagIdsToRemove)

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Relationships/UpdateToOneRelationshipTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public async Task Can_clear_OneToOne_relationship()
6666

6767
await _testContext.RunOnDatabaseAsync(async dbContext =>
6868
{
69-
dbContext.Groups.AddRange(existingGroup);
69+
dbContext.Groups.Add(existingGroup);
7070
await dbContext.SaveChangesAsync();
7171
});
7272

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/UpdateToOneRelationshipTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public async Task Can_clear_OneToOne_relationship()
209209

210210
await _testContext.RunOnDatabaseAsync(async dbContext =>
211211
{
212-
dbContext.RgbColors.AddRange(existingColor);
212+
dbContext.RgbColors.Add(existingColor);
213213
await dbContext.SaveChangesAsync();
214214
});
215215

0 commit comments

Comments
 (0)