diff --git a/src/Volo.Abp.Core/System/Collections/Generic/AbpCollectionExtensions.cs b/src/Volo.Abp.Core/System/Collections/Generic/AbpCollectionExtensions.cs
index 0750dbc41e..4240d895ba 100644
--- a/src/Volo.Abp.Core/System/Collections/Generic/AbpCollectionExtensions.cs
+++ b/src/Volo.Abp.Core/System/Collections/Generic/AbpCollectionExtensions.cs
@@ -20,7 +20,7 @@ namespace System.Collections.Generic
///
/// Adds an item to the collection if it's not already in the collection.
///
- /// Collection
+ /// The collection
/// Item to check and add
/// Type of the items in the collection
/// Returns True if added, returns False if not.
@@ -38,7 +38,30 @@ namespace System.Collections.Generic
}
///
- /// Removed all items from a collection those satisfy the given .
+ /// Adds an item to the collection if it's not already in the collection based on the given .
+ ///
+ /// The collection
+ /// The condition to decide if the item is already in the collection
+ /// A factory that returns the item
+ /// Type of the items in the collection
+ /// Returns True if added, returns False if not.
+ public static bool AddIfNotContains([NotNull] this ICollection source, [NotNull] Func predicate, [NotNull] Func itemFactory)
+ {
+ Check.NotNull(source, nameof(source));
+ Check.NotNull(source, nameof(predicate));
+ Check.NotNull(itemFactory, nameof(itemFactory));
+
+ if (source.Any(predicate))
+ {
+ return false;
+ }
+
+ source.Add(itemFactory());
+ return true;
+ }
+
+ ///
+ /// Removed all items from the collection those satisfy the given .
///
/// Type of the items in the collection
/// The collection
diff --git a/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpCollectionExtensions_Tests.cs b/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpCollectionExtensions_Tests.cs
new file mode 100644
index 0000000000..937eeb6c92
--- /dev/null
+++ b/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpCollectionExtensions_Tests.cs
@@ -0,0 +1,26 @@
+using Shouldly;
+using Xunit;
+
+namespace System.Collections.Generic
+{
+ public class AbpCollectionExtensions_Tests
+ {
+ [Fact]
+ public void AddIfNotContains_With_Predicate()
+ {
+ var collection = new List {4, 5, 6};
+
+ collection.AddIfNotContains(x => x == 5, () => 5);
+ collection.Count.ShouldBe(3);
+
+ collection.AddIfNotContains(x => x == 42, () => 42);
+ collection.Count.ShouldBe(4);
+
+ collection.AddIfNotContains(x => x < 8, () => 8);
+ collection.Count.ShouldBe(4);
+
+ collection.AddIfNotContains(x => x > 999, () => 8);
+ collection.Count.ShouldBe(5);
+ }
+ }
+}