Skip to content

Commit

Permalink
feat: Add System.Linq WhereNotNull() extension (#100)
Browse files Browse the repository at this point in the history
This solves the case of `foreach (string folder in folders.Where(x =>
!string.IsNullOrWhiteSpace(x)))` not working by itself and needing to
cast it or similar
  • Loading branch information
BenjaminMichaelis authored Nov 30, 2023
1 parent 4e18719 commit 00996bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions IntelliTect.Multitool.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.github\workflows\deploy.yml = .github\workflows\deploy.yml
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
global.json = global.json
README.md = README.md
EndProjectSection
EndProject
Expand Down
18 changes: 18 additions & 0 deletions IntelliTect.Multitool/Extensions/SystemLinqExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace IntelliTect.Multitool.Extensions;

/// <summary>
/// Various Linq extensions
/// </summary>
public static class SystemLinqExtensions
{
/// <summary>
/// Filters a sequence of values to only those that are not null.
/// </summary>
/// <typeparam name="T">The type of the elements of source.</typeparam>
/// <param name="source">A <see cref="IEnumerable{T}"/> to filter.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains elements from the input that are not null.</returns>
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source) where T : class?
{
return (IEnumerable<T>)source.Where(item => item is not null);
}
}

0 comments on commit 00996bd

Please sign in to comment.