Kritikos.Extensions.Linq 0.1.7

dotnet add package Kritikos.Extensions.Linq --version 0.1.7                
NuGet\Install-Package Kritikos.Extensions.Linq -Version 0.1.7                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Kritikos.Extensions.Linq" Version="0.1.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Kritikos.Extensions.Linq --version 0.1.7                
#r "nuget: Kritikos.Extensions.Linq, 0.1.7"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Kritikos.Extensions.Linq as a Cake Addin
#addin nuget:?package=Kritikos.Extensions.Linq&version=0.1.7

// Install Kritikos.Extensions.Linq as a Cake Tool
#tool nuget:?package=Kritikos.Extensions.Linq&version=0.1.7                

Extensions.Linq

Build Status Nuget Coverage Status codecov Quality Gate Status License GitHub language count GitHub top language

Opinionated System.Linq extensions for common operation, favoring fail fast philosophy.

-If type extensions

  • WhereIf
  • TakeIf
  • SkipIf

Allows queueing of multiple linq queries naturally instead of breaking the flow to provide condition checking. Typical example in an API trying to implement queries:

var query = DbContext.EntityName.Where(x=>true);

if (!string.IsNullOrEmpty(parameters.NameFilter)) {
  query = query.Where(x=> x.Name.Contains(parametersNameFilter))
}

    [...]

Apart from the cognitive issues added by such a syntax, developers new to Entity Framework or hasty code writing might end up in something resembling

var query = DbContext.EntityName.AsQueryable()

This is a major issue, as the extension method AsQueryable is not available in DbSet or IQueryable, but in IEnumerable, to which both DbSet and Queryable can be converted implicitly. This would force loading data for the entire table, and even worse, leaves an open database cursor, eventually leading to availability issues.

Compare this to the safe and natural Linq syntax provided by these methods

var query = DbContext.EntityName
  .WhereIf(
    !string.IsNullOrEmpty(parameters.NameFilter),
    x=> x.Name.Contains(parametersNameFilter))

    [...]

Ordering Extensions

  • OrderByProperty
  • OrderByPropertyDescending
  • ThenByProperty
  • ThenByPropertyDescending

Provides a set of methods allowing ordering a queryable by a property name (passed in as string). Also, an overload accepting a fallback selector is provided, in case the property does not exist on the specified type, or the property string is not populated.

Important information: As it should be apparent, this method of ordering relies on reflection. However, leveraging the dynamic keyword, polymorphic inline caching is used, so the reflection cost is paid only for the first time any such extension method is called for each type.

Initial implementation was a collaboration with palladin

Pagination Extensions

  • Slice

Simple and safe method to extract pages from Queryable datasets. Only available on IOrderedQueryable since this is the only way data parity can be enforced.

Deconstruction Extensions

Provides deconstruct extension methods for all expressions under the System.Linq.Expressions namespace, for easy pattern matching, especially when leveraging the swich expression new to C# 8.0. Requested, and guided, by palladin.

Feature was developed to faciliate the following kind of syntax, in order to provide the base for expression splicing, collaborated under nessos/Splicer:

expr = x => x + initial;

var restructured = expr switch
{
  LambdaExpression(var param, BinaryExpression(ExpressionType.Add, var left, ConstantExpression(ExpressionType.Constant, _, initial)))
    => Expression.Lambda(Expression.Add(left, Expression.Constant(addInstead)), param.ToArray()),

  _
    => throw new NotSupportedException()
};

Contributors

  • palladin, aka @NickPalladinos, high priest of the Old Ones, providing tips and guidance in return for blood sacrifice.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.7 1,219 12/18/2019
0.1.3 254 2/17/2023