From 1b58e150263c9fab85230105faafdcc7baf10d86 Mon Sep 17 00:00:00 2001 From: Jean HAKIZ'A Date: Fri, 6 May 2022 21:50:37 +0200 Subject: [PATCH 1/4] Adding Whereif for IQueryable and IEnumerable, finally their tests --- Lincolle4net.Tests/CollectionsUnitTest.cs | 35 +++++++++++++++++++ Lincolle4net.Tests/Lincolle4net.Tests.csproj | 21 ++++++++++++ Lincolle4net.Tests/LinqsUnitTest.cs | 36 ++++++++++++++++++++ Lincolle4net.sln | 31 +++++++++++++++++ Lincolle4net/Collections/WhereIfExtension.cs | 14 ++++++++ Lincolle4net/Lincolle4net.csproj | 9 +++++ Lincolle4net/Linqs/WhereIfExtension.cs | 19 +++++++++++ 7 files changed, 165 insertions(+) create mode 100644 Lincolle4net.Tests/CollectionsUnitTest.cs create mode 100644 Lincolle4net.Tests/Lincolle4net.Tests.csproj create mode 100644 Lincolle4net.Tests/LinqsUnitTest.cs create mode 100644 Lincolle4net.sln create mode 100644 Lincolle4net/Collections/WhereIfExtension.cs create mode 100644 Lincolle4net/Lincolle4net.csproj create mode 100644 Lincolle4net/Linqs/WhereIfExtension.cs diff --git a/Lincolle4net.Tests/CollectionsUnitTest.cs b/Lincolle4net.Tests/CollectionsUnitTest.cs new file mode 100644 index 0000000..a475ad0 --- /dev/null +++ b/Lincolle4net.Tests/CollectionsUnitTest.cs @@ -0,0 +1,35 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using System.Collections.Generic; +using System.Linq; + +namespace Lincolle4net.Tests +{ + [TestClass] + public class CollectionsUnitTest + { + [TestMethod] + public void ValuedFilter() + { + var filter = "DRC"; + var countries = (new List() { "USA", "DRC", "Uganda" }) + .AsEnumerable() + .WhereIf(!string.IsNullOrEmpty(filter), c => c == filter); + + Assert.IsNotNull(filter); + Assert.AreEqual(countries.Count(), 1); + } + + [TestMethod] + public void NullOrEmptyFilter() + { + var filter = string.Empty; + var countries = (new List() { "DRC", "Uganda", "USA" }) + .AsEnumerable() + .WhereIf(!string.IsNullOrEmpty(filter), c => c == filter); + + Assert.IsTrue(string.IsNullOrEmpty(filter)); + Assert.AreEqual(countries.Count(), 3); + } + } +} \ No newline at end of file diff --git a/Lincolle4net.Tests/Lincolle4net.Tests.csproj b/Lincolle4net.Tests/Lincolle4net.Tests.csproj new file mode 100644 index 0000000..5c3358a --- /dev/null +++ b/Lincolle4net.Tests/Lincolle4net.Tests.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + + false + + + + + + + + + + + + + + diff --git a/Lincolle4net.Tests/LinqsUnitTest.cs b/Lincolle4net.Tests/LinqsUnitTest.cs new file mode 100644 index 0000000..4cb6e59 --- /dev/null +++ b/Lincolle4net.Tests/LinqsUnitTest.cs @@ -0,0 +1,36 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using System.Collections.Generic; +using System.Linq; + +namespace Lincolle4net.Tests +{ + [TestClass] + public class LinqsUnitTest + { + + [TestMethod] + public void ValuedFilter() + { + var filter = "DRC"; + var countries = (new List() { "USA", "DRC", "Uganda" }) + .AsQueryable() + .WhereIf(!string.IsNullOrEmpty(filter), c => c == filter); + + Assert.IsNotNull(filter); + Assert.AreEqual(countries.Count(), 1); + } + + [TestMethod] + public void NullOrEmptyFilter() + { + var filter = string.Empty; + var countries = (new List() { "DRC", "Uganda", "USA" }) + .AsQueryable() + .WhereIf(!string.IsNullOrEmpty(filter), c => c == filter); + + Assert.IsTrue(string.IsNullOrEmpty(filter)); + Assert.AreEqual(countries.Count(), 3); + } + } +} \ No newline at end of file diff --git a/Lincolle4net.sln b/Lincolle4net.sln new file mode 100644 index 0000000..f8f1908 --- /dev/null +++ b/Lincolle4net.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32228.430 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lincolle4net", "Lincolle4net\Lincolle4net.csproj", "{73DEE913-6891-48DA-A336-BCAD599BCF9C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lincolle4net.Tests", "Lincolle4net.Tests\Lincolle4net.Tests.csproj", "{F338A189-9E49-4DEB-9F78-F655E7F371F9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {73DEE913-6891-48DA-A336-BCAD599BCF9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {73DEE913-6891-48DA-A336-BCAD599BCF9C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {73DEE913-6891-48DA-A336-BCAD599BCF9C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {73DEE913-6891-48DA-A336-BCAD599BCF9C}.Release|Any CPU.Build.0 = Release|Any CPU + {F338A189-9E49-4DEB-9F78-F655E7F371F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F338A189-9E49-4DEB-9F78-F655E7F371F9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F338A189-9E49-4DEB-9F78-F655E7F371F9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F338A189-9E49-4DEB-9F78-F655E7F371F9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7D8FEE18-19FC-4F93-999E-C05013AFEDC8} + EndGlobalSection +EndGlobal diff --git a/Lincolle4net/Collections/WhereIfExtension.cs b/Lincolle4net/Collections/WhereIfExtension.cs new file mode 100644 index 0000000..ef31b37 --- /dev/null +++ b/Lincolle4net/Collections/WhereIfExtension.cs @@ -0,0 +1,14 @@ +namespace System.Collections.Generic +{ + public static class WhereIfExtension + { + public static IEnumerable WhereIf(this IEnumerable input, bool condition, Func predicate) + where T : class + { + return condition ? + input.Where(predicate) : + input; + } + + } +} diff --git a/Lincolle4net/Lincolle4net.csproj b/Lincolle4net/Lincolle4net.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/Lincolle4net/Lincolle4net.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/Lincolle4net/Linqs/WhereIfExtension.cs b/Lincolle4net/Linqs/WhereIfExtension.cs new file mode 100644 index 0000000..7d26049 --- /dev/null +++ b/Lincolle4net/Linqs/WhereIfExtension.cs @@ -0,0 +1,19 @@ +using System; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace System.Linq +{ + public static class WhereIfExtension + { + public static IEnumerable WhereIf(this IQueryable input, bool condition, Func predicate) + where T : class + { + return condition ? + input.Where(predicate) : + input; + } + + } +} From de26dc4c438ea3f872e9b2f008dd5bcbf677b6b5 Mon Sep 17 00:00:00 2001 From: Jean HAKIZ'A Date: Sat, 7 May 2022 09:49:33 +0200 Subject: [PATCH 2/4] Adding Stack and dictionary with their tests --- Lincolle4net.Tests/CollectionsUnitTest.cs | 77 +++++++++++++++++++- Lincolle4net.Tests/LinqsUnitTest.cs | 4 +- Lincolle4net/Collections/WhereIfExtension.cs | 38 +++++++++- 3 files changed, 111 insertions(+), 8 deletions(-) diff --git a/Lincolle4net.Tests/CollectionsUnitTest.cs b/Lincolle4net.Tests/CollectionsUnitTest.cs index a475ad0..31c4ced 100644 --- a/Lincolle4net.Tests/CollectionsUnitTest.cs +++ b/Lincolle4net.Tests/CollectionsUnitTest.cs @@ -9,7 +9,7 @@ namespace Lincolle4net.Tests public class CollectionsUnitTest { [TestMethod] - public void ValuedFilter() + public void ValuedEnumerableFilter() { var filter = "DRC"; var countries = (new List() { "USA", "DRC", "Uganda" }) @@ -21,7 +21,7 @@ public void ValuedFilter() } [TestMethod] - public void NullOrEmptyFilter() + public void NullOrEmptyEnumerableFilter() { var filter = string.Empty; var countries = (new List() { "DRC", "Uganda", "USA" }) @@ -31,5 +31,76 @@ public void NullOrEmptyFilter() Assert.IsTrue(string.IsNullOrEmpty(filter)); Assert.AreEqual(countries.Count(), 3); } + + [TestMethod] + public void NullOrEmptyListFilter() + { + var filter = string.Empty; + var countries = (new List() { "DRC", "Uganda", "USA" }) + .WhereIf(!string.IsNullOrEmpty(filter), c => c == filter); + + Assert.IsTrue(string.IsNullOrEmpty(filter)); + Assert.AreEqual(countries.Count(), 3); + } + + [TestMethod] + public void ValuedCollectionFilter() + { + var filter = "da"; + var data = new List() { "Canada", "DRC", "Uganda", "USA" }; + + var countries = ((ICollection)(data)) + .WhereIf(!string.IsNullOrEmpty(filter), c => c.Contains(filter)); + + Assert.IsFalse(string.IsNullOrEmpty(filter)); + Assert.AreEqual(countries.Count(), 2); + } + + [TestMethod] + public void ValuedDictionaryFilter() + { + var dic = new Dictionary + { + { "test", "val" }, + { "test2", "val2" } + }; + var filter = "da"; + + var data = dic + .WhereIf(!string.IsNullOrEmpty(filter), c => c.Value == filter); + + Assert.IsFalse(string.IsNullOrEmpty(filter)); + Assert.AreEqual(data.Count(), 0); + + filter = "val"; + data = dic + .WhereIf(!string.IsNullOrEmpty(filter), c => c.Value == filter); + + Assert.AreEqual(data.Count(), 1); + Assert.AreNotEqual(data.Count(), 2); + } + + [TestMethod] + public void ValuedStackFilter() + { + Stack stk = new Stack(); + stk.Push(1); + stk.Push(2); + stk.Push(3); + // + var filter = 2; + + var data = stk + .WhereIf(filter > 0, c => c == filter); + + Assert.AreEqual(data.Count(), 1); + + filter = 3; + data = stk + .WhereIf(filter > 0, c => c < filter); + + Assert.AreEqual(data.Count(), 2); + } + } -} \ No newline at end of file +} diff --git a/Lincolle4net.Tests/LinqsUnitTest.cs b/Lincolle4net.Tests/LinqsUnitTest.cs index 4cb6e59..a908717 100644 --- a/Lincolle4net.Tests/LinqsUnitTest.cs +++ b/Lincolle4net.Tests/LinqsUnitTest.cs @@ -10,7 +10,7 @@ public class LinqsUnitTest { [TestMethod] - public void ValuedFilter() + public void ValuedQueryableFilter() { var filter = "DRC"; var countries = (new List() { "USA", "DRC", "Uganda" }) @@ -22,7 +22,7 @@ public void ValuedFilter() } [TestMethod] - public void NullOrEmptyFilter() + public void NullOrEmptyQueryableFilter() { var filter = string.Empty; var countries = (new List() { "DRC", "Uganda", "USA" }) diff --git a/Lincolle4net/Collections/WhereIfExtension.cs b/Lincolle4net/Collections/WhereIfExtension.cs index ef31b37..12091ad 100644 --- a/Lincolle4net/Collections/WhereIfExtension.cs +++ b/Lincolle4net/Collections/WhereIfExtension.cs @@ -3,10 +3,42 @@ public static class WhereIfExtension { public static IEnumerable WhereIf(this IEnumerable input, bool condition, Func predicate) - where T : class { - return condition ? - input.Where(predicate) : + return condition ? + input.Where(predicate) : + input; + } + + public static IEnumerable WhereIf(this IList input, bool condition, Func predicate) + { + return condition ? + input.Where(predicate) : + input; + } + + public static IEnumerable WhereIf(this ICollection input, bool condition, Func predicate) + { + return condition ? + input.Where(predicate) : + input; + } + + public static IEnumerable WhereIf(this Stack input, bool condition, Func predicate) + { + return condition ? + input.Where(predicate) : + input; + } + + public static Dictionary WhereIf(this Dictionary input, bool condition, Func, bool> predicate) + { + if (input is null) + throw new ArgumentNullException(nameof(input)); + + return condition ? + Enumerable + .Where(input, predicate) + .ToDictionary(item => item.Key, item => item.Value) : input; } From 7f09caf3e98198dac05b34703465958bb595b563 Mon Sep 17 00:00:00 2001 From: Jean HAKIZ'A Date: Sun, 8 May 2022 12:54:47 +0200 Subject: [PATCH 3/4] Removing unused references --- Lincolle4net/Linqs/WhereIfExtension.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Lincolle4net/Linqs/WhereIfExtension.cs b/Lincolle4net/Linqs/WhereIfExtension.cs index 7d26049..36bbdb8 100644 --- a/Lincolle4net/Linqs/WhereIfExtension.cs +++ b/Lincolle4net/Linqs/WhereIfExtension.cs @@ -1,9 +1,4 @@ -using System; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace System.Linq +namespace System.Linq { public static class WhereIfExtension { From 95ad5eb9f40f7e574552386f88919986348abf1a Mon Sep 17 00:00:00 2001 From: Hakiza Jean <30447830+Jhakiz@users.noreply.github.com> Date: Sun, 8 May 2022 19:38:15 +0200 Subject: [PATCH 4/4] Initial Readme commit --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0171c83..9a9477e 100644 --- a/README.md +++ b/README.md @@ -1 +1,51 @@ -# Lincolle4net \ No newline at end of file +# Lincolle4net + +By importing this library in your project, you are going to access some methods as extensions which will help you while filtering Linq and Collections in C#. + +For now the current version supports only `WhereIf` on **List**, **Enumerable**, **Queryable**, **Stack**, **Dictionary** and **Collection**. + +```csharp +var filter = "DRC"; +var countries = (new List() { "USA", "Canada" ,"DRC", "Uganda", "China" }) + .AsQueryable() + .WhereIf(!string.IsNullOrEmpty(filter), c => c == filter); +``` + +Note that, on each call of `WhereIf`, an inline `IF` condition is applied on specified generics. Without using this package, the above code could be written as below: + +```csharp +var filter = "DRC"; +var countries = (new List() { "USA", "Canada" ,"DRC", "Uganda", "China" }) + .AsQueryable(); + +if(!string.IsNullOrEmpty(filter)) +{ + countries.Where(c => c == filter); +} + +``` + +Use this library while filtering Dictionaries. + +```csharp +var dic = new Dictionary +{ + { "test", "val" }, + { "test2", "val2" } +}; +var filter = "va"; + +var data = dic.WhereIf(!string.IsNullOrEmpty(filter), c => c.Value.StartsWith(filter)); +``` + +### Roadmap + +- [x] **WhereIf** +- [ ] AddIf +- [ ] RemoveIf + +### Contribution + +The project is opened for community contribution. To do this, you've to fork this project, perform all operation, finaly push changes on your forked project. + +> Create a pull request if you want your changes to be merged to the main branch.