diff --git a/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs b/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs index 171a6ac237..a123236a4e 100644 --- a/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs +++ b/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs @@ -32,6 +32,18 @@ namespace System.Collections.Generic source.Insert(source.Count, item); } + public static void InsertAfter(this IList source, T existingItem, T item) + { + var index = source.IndexOf(existingItem); + if (index < 0) + { + source.AddFirst(item); + return; + } + + source.Insert(index + 1, item); + } + public static void InsertAfter(this IList source, Predicate selector, T item) { var index = source.FindIndex(selector); @@ -44,6 +56,17 @@ namespace System.Collections.Generic source.Insert(index + 1, item); } + public static void InsertBefore(this IList source, T existingItem, T item) + { + var index = source.IndexOf(existingItem); + if (index < 0) + { + source.AddLast(item); + return; + } + + source.Insert(index, item); + } public static void InsertBefore(this IList source, Predicate selector, T item) { diff --git a/framework/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs b/framework/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs index cc65099a77..93e2ef5824 100644 --- a/framework/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs +++ b/framework/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs @@ -11,6 +11,29 @@ namespace System.Collections.Generic { var list = Enumerable.Range(1, 3).ToList(); + list.InsertAfter(2, 42); + + list.Count.ShouldBe(4); + list[0].ShouldBe(1); + list[1].ShouldBe(2); + list[2].ShouldBe(42); + list[3].ShouldBe(3); + + list.InsertAfter(3, 43); + + list.Count.ShouldBe(5); + list[0].ShouldBe(1); + list[1].ShouldBe(2); + list[2].ShouldBe(42); + list[3].ShouldBe(3); + list[4].ShouldBe(43); + } + + [Fact] + public void InsertAfter_With_Predicate() + { + var list = Enumerable.Range(1, 3).ToList(); + list.InsertAfter(i => i == 2, 42); list.Count.ShouldBe(4); @@ -30,7 +53,7 @@ namespace System.Collections.Generic } [Fact] - public void InsertAfter_Should_Insert_To_First_If_Not_Found() + public void InsertAfter_With_Predicate_Should_Insert_To_First_If_Not_Found() { var list = Enumerable.Range(1, 3).ToList(); @@ -48,6 +71,29 @@ namespace System.Collections.Generic { var list = Enumerable.Range(1, 3).ToList(); + list.InsertBefore(2, 42); + + list.Count.ShouldBe(4); + list[0].ShouldBe(1); + list[1].ShouldBe(42); + list[2].ShouldBe(2); + list[3].ShouldBe(3); + + list.InsertBefore(1, 43); + + list.Count.ShouldBe(5); + list[0].ShouldBe(43); + list[1].ShouldBe(1); + list[2].ShouldBe(42); + list[3].ShouldBe(2); + list[4].ShouldBe(3); + } + + [Fact] + public void InsertBefore_With_Predicate() + { + var list = Enumerable.Range(1, 3).ToList(); + list.InsertBefore(i => i == 2, 42); list.Count.ShouldBe(4);