Skip to content

Commit 1fb69ae

Browse files
author
Bart Koelman
committed
Removed unneeded ToList/ToArray calls
1 parent 90c2432 commit 1fb69ae

File tree

11 files changed

+30
-33
lines changed

11 files changed

+30
-33
lines changed

src/JsonApiDotNetCore/Controllers/JsonApiControllerMixin.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using JsonApiDotNetCore.Middleware;
43
using JsonApiDotNetCore.Models.JsonApiDocuments;
54
using Microsoft.AspNetCore.Mvc;
@@ -16,7 +15,7 @@ protected IActionResult Error(Error error)
1615

1716
protected IActionResult Error(IEnumerable<Error> errors)
1817
{
19-
var document = new ErrorDocument(errors.ToList());
18+
var document = new ErrorDocument(errors);
2019

2120
return new ObjectResult(document)
2221
{

src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private void DetachRelationships(TResource entity)
172172

173173
if (value is IEnumerable<IIdentifiable> collection)
174174
{
175-
foreach (IIdentifiable single in collection.ToList())
175+
foreach (IIdentifiable single in collection)
176176
_context.Entry(single).State = EntityState.Detached;
177177
// detaching has many relationships is not sufficient to
178178
// trigger a full reload of relationships: the navigation
@@ -255,7 +255,6 @@ private IEnumerable GetTrackedManyRelationshipValue(IEnumerable<IIdentifiable> r
255255
if (tracked != null) newWasAlreadyAttached = true;
256256
return Convert.ChangeType(tracked ?? pointer, relationshipAttr.RightType);
257257
})
258-
.ToList()
259258
.CopyToTypedCollection(relationshipAttr.PropertyInfo.PropertyType);
260259

261260
if (newWasAlreadyAttached) wasAlreadyAttached = true;

src/JsonApiDotNetCore/Internal/Contracts/IResourceContextProvider.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using System.Collections.Generic;
23
using JsonApiDotNetCore.Models;
34

45
namespace JsonApiDotNetCore.Internal.Contracts
@@ -11,7 +12,7 @@ public interface IResourceContextProvider
1112
/// <summary>
1213
/// Gets all registered context entities
1314
/// </summary>
14-
ResourceContext[] GetResourceContexts();
15+
IEnumerable<ResourceContext> GetResourceContexts();
1516

1617
/// <summary>
1718
/// Get the resource metadata by the DbSet property name
@@ -28,4 +29,4 @@ public interface IResourceContextProvider
2829
/// </summary>
2930
ResourceContext GetResourceContext<TResource>() where TResource : class, IIdentifiable;
3031
}
31-
}
32+
}

src/JsonApiDotNetCore/Internal/ResourceGraph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public ResourceGraph(List<ResourceContext> resources)
2020
}
2121

2222
/// <inheritdoc />
23-
public ResourceContext[] GetResourceContexts() => Resources.ToArray();
23+
public IEnumerable<ResourceContext> GetResourceContexts() => Resources;
2424
/// <inheritdoc />
2525
public ResourceContext GetResourceContext(string resourceName)
2626
=> Resources.SingleOrDefault(e => e.ResourceName == resourceName);

src/JsonApiDotNetCore/Serialization/Server/Builders/ResponseResourceObjectBuilder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ protected override RelationshipEntry GetRelationshipData(RelationshipAttribute r
6969
private bool ShouldInclude(RelationshipAttribute relationship, out List<List<RelationshipAttribute>> inclusionChain)
7070
{
7171
inclusionChain = _includeService.Get()?.Where(l => l.First().Equals(relationship)).ToList();
72-
if (inclusionChain == null || !inclusionChain.Any())
73-
return false;
74-
return true;
72+
return inclusionChain != null && inclusionChain.Any();
7573
}
7674
}
7775
}

test/IntegrationTests/Data/EntityRepositoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private AppDbContext GetContext(Guid? seed = null)
176176
.Options;
177177
var context = new AppDbContext(options);
178178

179-
context.TodoItems.RemoveRange(context.TodoItems.ToList());
179+
context.TodoItems.RemoveRange(context.TodoItems);
180180
return context;
181181
}
182182

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/PagingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public async Task Pagination_WithPageSizeAndPageNumber_ReturnsCorrectSubsetOfRes
3737
const int expectedEntitiesPerPage = 2;
3838
var totalCount = expectedEntitiesPerPage * 2;
3939
var person = new Person();
40-
var todoItems = _todoItemFaker.Generate(totalCount).ToList();
40+
var todoItems = _todoItemFaker.Generate(totalCount);
4141
foreach (var todoItem in todoItems)
4242
{
4343
todoItem.Owner = person;
@@ -82,7 +82,7 @@ public async Task Pagination_OnGivenPage_DisplaysCorrectTopLevelLinks(int pageNu
8282
{
8383
LastName = "&Ampersand"
8484
};
85-
var todoItems = _todoItemFaker.Generate(totalCount).ToList();
85+
var todoItems = _todoItemFaker.Generate(totalCount);
8686

8787
foreach (var todoItem in todoItems)
8888
todoItem.Owner = person;

test/JsonApiDotNetCoreExampleTests/Acceptance/TodoItemsControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public TodoItemControllerTests(TestFixture<Startup> fixture)
4747
public async Task Can_Get_TodoItems_Paginate_Check()
4848
{
4949
// Arrange
50-
_context.TodoItems.RemoveRange(_context.TodoItems.ToList());
50+
_context.TodoItems.RemoveRange(_context.TodoItems);
5151
_context.SaveChanges();
5252
int expectedEntitiesPerPage = _fixture.GetService<IJsonApiOptions>().DefaultPageSize;
5353
var person = new Person();

test/UnitTests/ResourceHooks/AffectedEntitiesHelperTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ public void RelationshipsDictionary_GetAffected()
8989
var affectedThroughToMany = relationshipsDictionary.GetAffected(d => d.ToManies).ToList();
9090

9191
// Assert
92-
affectedThroughFirstToOne.ForEach((entity) => Assert.Contains(entity, FirstToOnesEntities));
93-
affectedThroughSecondToOne.ForEach((entity) => Assert.Contains(entity, SecondToOnesEntities));
94-
affectedThroughToMany.ForEach((entity) => Assert.Contains(entity, ToManiesEntities));
92+
affectedThroughFirstToOne.ForEach(entity => Assert.Contains(entity, FirstToOnesEntities));
93+
affectedThroughSecondToOne.ForEach(entity => Assert.Contains(entity, SecondToOnesEntities));
94+
affectedThroughToMany.ForEach(entity => Assert.Contains(entity, ToManiesEntities));
9595
}
9696

9797
[Fact]
@@ -178,9 +178,9 @@ public void EntityDiff_GetAffected_Relationships()
178178
var affectedThroughToMany = diffs.GetAffected(d => d.ToManies).ToList();
179179

180180
// Assert
181-
affectedThroughFirstToOne.ForEach((entity) => Assert.Contains(entity, FirstToOnesEntities));
182-
affectedThroughSecondToOne.ForEach((entity) => Assert.Contains(entity, SecondToOnesEntities));
183-
affectedThroughToMany.ForEach((entity) => Assert.Contains(entity, ToManiesEntities));
181+
affectedThroughFirstToOne.ForEach(entity => Assert.Contains(entity, FirstToOnesEntities));
182+
affectedThroughSecondToOne.ForEach(entity => Assert.Contains(entity, SecondToOnesEntities));
183+
affectedThroughToMany.ForEach(entity => Assert.Contains(entity, ToManiesEntities));
184184
}
185185

186186
[Fact]
@@ -195,8 +195,8 @@ public void EntityDiff_GetAffected_Attributes()
195195
DiffableEntityHashSet<Dummy> diffs = new DiffableEntityHashSet<Dummy>(AllEntities, dbEntities, Relationships, updatedAttributes);
196196

197197
// Act
198-
var affectedThroughSomeUpdatedProperty = diffs.GetAffected(d => d.SomeUpdatedProperty).ToList();
199-
var affectedThroughSomeNotUpdatedProperty = diffs.GetAffected(d => d.SomeNotUpdatedProperty).ToList();
198+
var affectedThroughSomeUpdatedProperty = diffs.GetAffected(d => d.SomeUpdatedProperty);
199+
var affectedThroughSomeNotUpdatedProperty = diffs.GetAffected(d => d.SomeNotUpdatedProperty);
200200

201201
// Assert
202202
Assert.NotEmpty(affectedThroughSomeUpdatedProperty);
@@ -213,17 +213,17 @@ private void AssertRelationshipDictionaryGetters(Dictionary<RelationshipAttribut
213213
Assert.Contains(ToManyAttr, toManies.Keys);
214214
Assert.Equal(relationshipsDictionary.Keys.Count, toOnes.Keys.Count + toManies.Keys.Count + notTargeted.Keys.Count);
215215

216-
toOnes[FirstToOneAttr].ToList().ForEach((entity) =>
216+
toOnes[FirstToOneAttr].ToList().ForEach(entity =>
217217
{
218218
Assert.Contains(entity, FirstToOnesEntities);
219219
});
220220

221-
toOnes[SecondToOneAttr].ToList().ForEach((entity) =>
221+
toOnes[SecondToOneAttr].ToList().ForEach(entity =>
222222
{
223223
Assert.Contains(entity, SecondToOnesEntities);
224224
});
225225

226-
toManies[ToManyAttr].ToList().ForEach((entity) =>
226+
toManies[ToManyAttr].ToList().ForEach(entity =>
227227
{
228228
Assert.Contains(entity, ToManiesEntities);
229229
});

test/UnitTests/ResourceHooks/ResourceHookExecutor/Delete/BeforeDelete_WithDbValue_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void BeforeDelete_No_Children_Hooks()
8888

8989
private bool CheckImplicitTodos(IRelationshipsDictionary<TodoItem> rh)
9090
{
91-
var todos = rh.GetByRelationship<Person>().ToList();
91+
var todos = rh.GetByRelationship<Person>();
9292
return todos.Count == 2;
9393
}
9494

0 commit comments

Comments
 (0)