mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
refactor CollectionExtensions
This commit is contained in:
parent
4d5f5ea65d
commit
9ed8220d1c
4 changed files with 20 additions and 30 deletions
17
src/FilterLists.Services/Extensions/CollectionExtensions.cs
Normal file
17
src/FilterLists.Services/Extensions/CollectionExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue