CollectionExtensionsInt
This commit is contained in:
parent
1a73253867
commit
9657e7449f
|
|
@ -22,18 +22,23 @@ namespace AyCode.Core.Extensions
|
||||||
if (source == null) throw new ArgumentNullException(nameof(source), $"source == null");
|
if (source == null) throw new ArgumentNullException(nameof(source), $"source == null");
|
||||||
if (dataItem.Id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(dataItem), "UpdateCollection->dataItem.Id.IsNullOrEmpty()");
|
if (dataItem.Id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(dataItem), "UpdateCollection->dataItem.Id.IsNullOrEmpty()");
|
||||||
|
|
||||||
var transferIndex = source.FindIndex(x => x.Id == dataItem.Id);
|
var index = source.FindIndex(x => x.Id == dataItem.Id);
|
||||||
|
|
||||||
|
return source.UpdateCollectionByIndex(index, dataItem, isRemove);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TrackingState UpdateCollectionByIndex<TDataItem>(this IList<TDataItem> source, int index, TDataItem dataItem, bool isRemove)
|
||||||
|
{
|
||||||
if (isRemove)
|
if (isRemove)
|
||||||
{
|
{
|
||||||
if (transferIndex > -1) source.RemoveAt(transferIndex);
|
if (index > -1) source.RemoveAt(index);
|
||||||
|
|
||||||
return TrackingState.Remove;
|
return TrackingState.Remove;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (transferIndex > -1)
|
if (index > -1)
|
||||||
{
|
{
|
||||||
source[transferIndex] = dataItem;
|
source[index] = dataItem;
|
||||||
return TrackingState.Update;
|
return TrackingState.Update;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
using AyCode.Core.Enums;
|
||||||
|
using AyCode.Core.Interfaces;
|
||||||
|
|
||||||
|
namespace AyCode.Core.Extensions;
|
||||||
|
|
||||||
|
public static class CollectionExtensionsInt
|
||||||
|
{
|
||||||
|
public static void UpdateCollection<TDataItem>(this IList<TDataItem> source, IList<TDataItem> dataItems, bool isRemove) where TDataItem : IId<int>
|
||||||
|
{
|
||||||
|
if (source == null) throw new ArgumentNullException(nameof(source), $"source == null");
|
||||||
|
if (dataItems == null) throw new ArgumentNullException(nameof(dataItems), $"dataItems == null");
|
||||||
|
|
||||||
|
foreach (var dataItem in dataItems)
|
||||||
|
{
|
||||||
|
source.UpdateCollection(dataItem, isRemove);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TrackingState UpdateCollection<TDataItem>(this IList<TDataItem> source, TDataItem dataItem, bool isRemove) where TDataItem : IId<int>
|
||||||
|
{
|
||||||
|
if (source == null) throw new ArgumentNullException(nameof(source), $"source == null");
|
||||||
|
|
||||||
|
var index = source.FindIndex(x => x.Id == dataItem.Id);
|
||||||
|
return source.UpdateCollectionByIndex(index, dataItem, isRemove);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue