refactor CollectionExtensions

This commit is contained in:
Collin M. Barrett 2018-08-15 08:09:07 -05:00
parent 4d5f5ea65d
commit 9ed8220d1c
4 changed files with 20 additions and 30 deletions

View file

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace FilterLists.Services.Extensions
{
public static class CollectionExtensions
{
//https://stackoverflow.com/a/26360010/2343739
public static void AddRange<T>(this ICollection<T> destination, IEnumerable<T> source)
{
if (destination is List<T> list)
list.AddRange(source);
else
foreach (var item in source)
destination.Add(item);
}
}
}

View file

@ -1,29 +0,0 @@
using System.Collections.Generic;
namespace FilterLists.Services.Extensions
{
public static class EnumerableExtensions
{
public static void AddRange<T>(this ICollection<T> destination, IEnumerable<T> source)
{
foreach (var item in source)
destination.Add(item);
}
public static IEnumerable<IEnumerable<T>> GetBatches<T>(this IEnumerable<T> source, int batchSize)
{
var batches = new List<T>(batchSize);
foreach (var item in source)
{
batches.Add(item);
if (batches.Count != batchSize)
continue;
yield return batches.AsReadOnly();
batches = new List<T>(batchSize);
}
if (batches.Count > 0)
yield return batches.AsReadOnly();
}
}
}

View file

@ -20,6 +20,7 @@
<PackageReference Include="JetBrains.Annotations" Version="2018.2.1" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.4.0" />
<PackageReference Include="Microsoft.DotNet.Analyzers.Compatibility" Version="0.2.12-alpha" />
<PackageReference Include="morelinq" Version="3.0.0" />
<PackageReference Include="SendGrid" Version="9.9.0" />
</ItemGroup>

View file

@ -9,6 +9,7 @@
using FilterLists.Data.Entities.Junctions;
using FilterLists.Services.Extensions;
using FilterLists.Services.Snapshot.Models;
using MoreLinq;
namespace FilterLists.Services.Snapshot
{
@ -135,7 +136,7 @@ private static IEnumerable<string> GetRawRules(string content)
}
private IEnumerable<SnapshotBatchDe> GetSnapshotBatches(IEnumerable<string> rawRules) =>
rawRules.GetBatches(BatchSize).Select(b => new SnapshotBatchDe(dbContext, snapshot, b));
rawRules.Batch(BatchSize).Select(b => new SnapshotBatchDe(dbContext, snapshot, b));
private static async Task SaveSnapshotBatches(IEnumerable<SnapshotBatchDe> snapshotBatches)
{