Skip to content

Commit 28687f3

Browse files
authored
Merge pull request #39 from I-RzR-I/feature/DictListAddIfNotExist
Feature/dict list add if not exist
2 parents e8decf1 + c73f077 commit 28687f3

11 files changed

Lines changed: 403 additions & 15 deletions

File tree

docs/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
### **v3.4.0.7452** [[RzR](mailto:108324929+I-RzR-I@users.noreply.github.com)] 07-10-2025
2+
* [c160a83] (RzR) -> Auto commit uncommited files
3+
* [b417765] (RzR) -> Add new enumeration extnesion: `AdddIfNotExist`.
4+
* [ab7bedc] (RzR) -> Add new dictionary extnesion: `IsNullOrEmpty`.
5+
* [1f20f4a] (RzR) -> Add new dictionary extnesion: `AdddIfNotExist`.
6+
* [0a7846f] (RzR) -> Add new array extnesion: `AppendIfNotExist`.
7+
18
### **v3.3.0.5249** [[RzR](mailto:108324929+I-RzR-I@users.noreply.github.com)] 29-09-2025
29
* [325ff42] (RzR) -> Auto commit uncommited files
310
* [7385809] (RzR) -> Add new Guid extension methods: `NullIfEmpty`, `EmptyIfNull`, `IsMissing`, `HasValidValue`.

src/DomainCommonExtensions/ArraysExtensions/ArrayExtensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ public static T[] AppendItem<T>(this T[] source, int index, T item)
136136
return source;
137137
}
138138

139+
/// -------------------------------------------------------------------------------------------------
140+
/// <summary>
141+
/// Append an item to source array.
142+
/// </summary>
143+
/// <typeparam name="T">Generic type parameter. Source type.</typeparam>
144+
/// <param name="source">Input source.</param>
145+
/// <param name="itemToAppend">The item to append.</param>
146+
/// <returns>
147+
/// Returns a new T[].
148+
/// </returns>
149+
/// =================================================================================================
150+
public static T[] AppendIfNotExist<T>(this T[] source, T itemToAppend)
151+
{
152+
if (source.IsNullOrEmptyEnumerable() && itemToAppend.IsNull()) return new T[] { };
153+
if (source.IsNullOrEmptyEnumerable()) return new[] { itemToAppend };
154+
if (itemToAppend.IsNull()) return source;
155+
156+
if (source.IndexOf(itemToAppend) == -1)
157+
return source;
158+
159+
return source.AppendItem(itemToAppend);
160+
}
161+
139162
/// -------------------------------------------------------------------------------------------------
140163
/// <summary>
141164
/// Append an items to source array.

src/DomainCommonExtensions/ArraysExtensions/CollectionExtensions.cs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
#region U S A G E S
1818

19+
using DomainCommonExtensions.DataTypeExtensions;
20+
using DomainCommonExtensions.Utilities.Ensure;
1921
using System;
2022
using System.Collections.Generic;
23+
using System.Collections.ObjectModel;
2124
using System.Collections.Specialized;
2225
using System.Linq;
23-
using DomainCommonExtensions.CommonExtensions;
2426

2527
#endregion
2628

@@ -41,12 +43,13 @@ public static class CollectionExtensions
4143
public static ICollection<TTarget> AddRange<TTarget>(this ICollection<TTarget> context,
4244
IEnumerable<TTarget> data)
4345
{
44-
if (context.IsNull()) return null;
45-
foreach (var item in data) context.Add(item);
46+
if (context.IsNullOrEmptyEnumerable()) return null;
47+
foreach (var item in data)
48+
context.Add(item);
4649

4750
return context;
4851
}
49-
52+
5053
/// <summary>
5154
/// Get distinct items by list propriety
5255
/// </summary>
@@ -58,17 +61,22 @@ public static ICollection<TTarget> AddRange<TTarget>(this ICollection<TTarget> c
5861
public static IEnumerable<TSource> DistinctBy<TSource, TKey>
5962
(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
6063
{
64+
DomainEnsure.IsNotNull(source, nameof(source));
65+
6166
var seenKeys = new HashSet<TKey>();
62-
foreach (var element in source.Where(element => seenKeys.Add(keySelector(element)))) yield return element;
67+
foreach (var element in source.Where(element => seenKeys.Add(keySelector(element))))
68+
yield return element;
6369
}
64-
70+
6571
/// <summary>
6672
/// NameValueCollection to KeyValuePair
6773
/// </summary>
6874
/// <param name="collection"></param>
6975
/// <returns></returns>
7076
public static IEnumerable<KeyValuePair<string, string>> ToKeyValuePair(this NameValueCollection collection)
7177
{
78+
DomainEnsure.IsNotNull(collection, nameof(collection));
79+
7280
return collection.AllKeys.Select(x => new KeyValuePair<string, string>(x, collection[x]));
7381
}
7482

@@ -94,8 +102,9 @@ public static bool In<T>(this T val, params T[] values) where T : struct
94102
/// <remarks></remarks>
95103
public static ICollection<T> With<T>(this ICollection<T> source, T item)
96104
{
105+
source ??= new Collection<T>();
97106
source.Add(item);
98-
107+
99108
return source;
100109
}
101110

@@ -109,8 +118,9 @@ public static ICollection<T> With<T>(this ICollection<T> source, T item)
109118
/// <remarks></remarks>
110119
public static ICollection<T> With<T>(this ICollection<T> source, T[] items)
111120
{
121+
source ??= new Collection<T>();
112122
source.AddRange(items);
113-
123+
114124
return source;
115125
}
116126

@@ -124,8 +134,9 @@ public static ICollection<T> With<T>(this ICollection<T> source, T[] items)
124134
/// <remarks></remarks>
125135
public static ICollection<T> Without<T>(this ICollection<T> source, T item)
126136
{
137+
source ??= new Collection<T>();
127138
source.Remove(item);
128-
139+
129140
return source;
130141
}
131142

@@ -139,8 +150,27 @@ public static ICollection<T> Without<T>(this ICollection<T> source, T item)
139150
/// <remarks></remarks>
140151
public static ICollection<T> WithMany<T>(this ICollection<T> source, IEnumerable<T> items)
141152
{
153+
source ??= new Collection<T>();
142154
source.AddRange(items);
143-
155+
156+
return source;
157+
}
158+
159+
/// <summary>
160+
/// An ICollection&lt;T&gt; extension method that adds if not exist 'item' to source collection.
161+
/// </summary>
162+
/// <typeparam name="T">Type of collection.</typeparam>
163+
/// <param name="source">Source collection.</param>
164+
/// <param name="item">Item to add.</param>
165+
/// <returns>
166+
/// A list of.
167+
/// </returns>
168+
public static ICollection<T> AddToCollectionIfNotExist<T>(this ICollection<T> source, T item)
169+
{
170+
source ??= new Collection<T>();
171+
if (source.Any(x => x.Equals(item)).IsFalse())
172+
source.Add(item);
173+
144174
return source;
145175
}
146176
}

src/DomainCommonExtensions/ArraysExtensions/DictionaryExtensions.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
#region U S A G E S
1818

19+
using DomainCommonExtensions.DataTypeExtensions;
1920
using System;
2021
using System.Collections;
2122
using System.Collections.Generic;
22-
using DomainCommonExtensions.DataTypeExtensions;
23+
using DomainCommonExtensions.CommonExtensions;
24+
using DomainCommonExtensions.Utilities.Ensure;
2325

2426
#endregion
2527

@@ -62,6 +64,8 @@ public static Dictionary<TKey, TValue> AddRange<TKey, TValue>(this Dictionary<TK
6264
public static Dictionary<TKey, TValue> RemoveKeys<TKey, TValue>(this Dictionary<TKey, TValue> dict,
6365
IEnumerable<TKey> keys)
6466
{
67+
DomainEnsure.IsNotNull(dict, nameof(dict));
68+
6569
foreach (var key in keys)
6670
if (dict.ContainsKey(key).IsTrue())
6771
dict.Remove(key);
@@ -77,13 +81,23 @@ public static Dictionary<TKey, TValue> RemoveKeys<TKey, TValue>(this Dictionary<
7781
/// <returns></returns>
7882
public static int IndexOf(this IDictionary dictionary, object value)
7983
{
84+
if (dictionary.IsNullOrEmpty()) return -1;
85+
8086
for (var i = 0; i < dictionary.Count; ++i)
8187
if (dictionary[i] == value)
8288
return i;
8389

8490
return -1;
8591
}
8692

93+
/// <summary>
94+
/// Check if Dictionary is null or empty (with no values)
95+
/// </summary>
96+
/// <param name="dictionary">source dictionary</param>
97+
/// <returns></returns>
98+
public static bool IsNullOrEmpty(this IDictionary dictionary)
99+
=> dictionary.IsNull() || dictionary.Count.IsZero();
100+
87101
/// <summary>
88102
/// Get STRING or default value
89103
/// </summary>
@@ -153,5 +167,42 @@ public static bool GetBoolOrDefault(this Dictionary<string, string> dictionary,
153167

154168
return @default;
155169
}
170+
171+
/// -------------------------------------------------------------------------------------------------
172+
/// <summary>
173+
/// An IDictionary&lt;TK,TV&gt; extension method that adds if not exist 'item' to source.
174+
/// </summary>
175+
/// <typeparam name="TKey">Type of the key.</typeparam>
176+
/// <typeparam name="TValue">Type of the value.</typeparam>
177+
/// <param name="source">The source to act on.</param>
178+
/// <param name="item">The item.</param>
179+
/// =================================================================================================
180+
public static void AddIfNotExist<TKey, TValue>(this IDictionary<TKey, TValue> source, KeyValuePair<TKey, TValue> item)
181+
{
182+
DomainEnsure.IsNotNull(source, nameof(source));
183+
184+
if (source.ContainsKey(item.Key)) return;
185+
186+
source[item.Key] = item.Value;
187+
}
188+
189+
/// -------------------------------------------------------------------------------------------------
190+
/// <summary>
191+
/// An IDictionary&lt;TK,TV&gt; extension method that adds if not exist 'item' to source.
192+
/// </summary>
193+
/// <typeparam name="TKey">Type of the key.</typeparam>
194+
/// <typeparam name="TValue">Type of the value.</typeparam>
195+
/// <param name="source">The source to act on.</param>
196+
/// <param name="key">Required. Search key.</param>
197+
/// <param name="value">.</param>
198+
/// =================================================================================================
199+
public static void AddIfNotExist<TKey, TValue>(this IDictionary<TKey, TValue> source, TKey key, TValue value)
200+
{
201+
DomainEnsure.IsNotNull(source, nameof(source));
202+
203+
if (source.ContainsKey(key)) return;
204+
205+
source[key] = value;
206+
}
156207
}
157208
}

src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,5 +575,23 @@ public static IEnumerable<IEnumerable<T>> Chunked<T>(this IEnumerable<T> source,
575575

576576
return chunks;
577577
}
578+
579+
/// <summary>
580+
/// Enumerates add if not exist in this collection.
581+
/// </summary>
582+
/// <typeparam name="T">Generic type parameter.</typeparam>
583+
/// <param name="source">Source Enumerate.</param>
584+
/// <param name="item">Item to add.</param>
585+
/// <returns>
586+
/// An enumerator that allows foreach to be used to process add if not exist in this
587+
/// collection.
588+
/// </returns>
589+
public static IEnumerable<T> AddIfNotExist<T>(this IEnumerable<T> source, T item)
590+
{
591+
if (source.Any(x => x.Equals(item)).IsFalse())
592+
source = source.Concat(new[] { item });
593+
594+
return source;
595+
}
578596
}
579597
}

src/DomainCommonExtensions/ArraysExtensions/ListExtensions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
#region U S A G E S
1818

19+
using DomainCommonExtensions.DataTypeExtensions;
1920
using System;
2021
using System.Collections.Generic;
2122
using System.Data;
22-
using DomainCommonExtensions.DataTypeExtensions;
2323

2424
#endregion
2525

@@ -112,5 +112,18 @@ public static void ActionForEach<T>(this List<T> source, Action<T> action)
112112
action(item);
113113
}
114114
}
115+
116+
/// <summary>
117+
/// A List&lt;T&gt; extension method that adds if not exist 'item' to source list.
118+
/// </summary>
119+
/// <typeparam name="T">Generic type parameter.</typeparam>
120+
/// <param name="source">Source list.</param>
121+
/// <param name="item">The item.</param>
122+
public static void AddToListIfNotExist<T>(this List<T> source, T item)
123+
{
124+
source ??= new List<T>();
125+
if (source.IndexOf(item) == -1)
126+
source.Add(item);
127+
}
115128
}
116129
}

src/shared/GeneralAssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
4848
#endif
4949

50-
[assembly: AssemblyVersion("3.3.0.5249")]
51-
[assembly: AssemblyFileVersion("3.3.0.5249")]
52-
[assembly: AssemblyInformationalVersion("3.3.0.5249")]
50+
[assembly: AssemblyVersion("3.4.0.7452")]
51+
[assembly: AssemblyFileVersion("3.4.0.7452")]
52+
[assembly: AssemblyInformationalVersion("3.4.0.7452")]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ***********************************************************************
2+
// Assembly : RzR.Shared.Extensions.DataTypeTests
3+
// Author : RzR
4+
// Created On : 2025-10-07 16:10
5+
//
6+
// Last Modified By : RzR
7+
// Last Modified On : 2025-10-07 16:56
8+
// ***********************************************************************
9+
// <copyright file="CollectionTests.cs" company="RzR SOFT & TECH">
10+
// Copyright © RzR. All rights reserved.
11+
// </copyright>
12+
//
13+
// <summary>
14+
// </summary>
15+
// ***********************************************************************
16+
17+
#region U S A G E S
18+
19+
using System.Collections.Generic;
20+
using DomainCommonExtensions.ArraysExtensions;
21+
using Microsoft.VisualStudio.TestTools.UnitTesting;
22+
23+
#endregion
24+
25+
namespace DataTypeTests.DataTests.Array
26+
{
27+
[TestClass]
28+
public class CollectionTests
29+
{
30+
[TestMethod]
31+
public void AddIsNotExist_Test()
32+
{
33+
var list = new List<string> { "Id", "Code" };
34+
35+
list.AddToCollectionIfNotExist("Name");
36+
37+
Assert.IsTrue(list.Count == 3);
38+
Assert.IsTrue(list.Contains("Name"));
39+
}
40+
41+
[TestMethod]
42+
public void AddIsNotExist_WithExistKey_Test()
43+
{
44+
var list = new List<string> { "Id", "Code", "Name" };
45+
46+
list.AddToCollectionIfNotExist("Name");
47+
48+
Assert.IsTrue(list.Count == 3);
49+
Assert.IsTrue(list.Contains("Name"));
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)