DeuceGear.Linq 1.0.3

dotnet add package DeuceGear.Linq --version 1.0.3
NuGet\Install-Package DeuceGear.Linq -Version 1.0.3
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="DeuceGear.Linq" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DeuceGear.Linq --version 1.0.3
#r "nuget: DeuceGear.Linq, 1.0.3"
#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 DeuceGear.Linq as a Cake Addin
#addin nuget:?package=DeuceGear.Linq&version=1.0.3

// Install DeuceGear.Linq as a Cake Tool
#tool nuget:?package=DeuceGear.Linq&version=1.0.3

<< Back to main page

DeuceGear.Linq

Extra features that may come in handy while using Linq.

Testdata

To make the DeuceGear.Linq samples easier, we use a basic class:

public class Sample
{
    public Sample(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
    public override bool Equals(object obj)
    {
        if (obj is Sample sample)
        {
            return string.Compare(sample.FirstName, FirstName) == 0
                && string.Compare(sample.LastName, LastName) == 0;
        }
        return base.Equals(obj);
    }
}

We alse defined a small list of objects:

Data = new List<Sample>()
{
    new Sample("Jose", "Rodriguez"),
    new Sample("Manuel", "Rivera"),
    new Sample("Julian", "Mendez"),
}.AsQueryable();

IEnumerable/IQueryable Extensions

IsIn(...) / IsNotIn(...)

Instead of writing:

var list = new List<string>() 
    {
        new Sample("Bob", "Barker")
        new Sample("Jose", "Rodriguez"),
        new Sample("Julian", "Mendez") 
    };
var result = TestData.List.Where(x => list.Contains(x));

It's now possible to write in a simpler syntax:

var result = TestData.List.IsIn(
    new Sample("Bob", "Barker"),
    new Sample("Jose", "Rodriguez"),
    new Sample("Julian", "Mendez"));
or
var result = TestData.List.IsIn(x => x.FirstName, "Bob", "Jose", "Julian");

OrderBy(specification, sortDirection)

IQueryable and IEnumerable can use the method OrderBy(specification, SortDirection). This way it's easy to determine the sort direction of a list without the need of an if and OrderBy or OrderByDescending logic.

var result = TestData.List.OrderBy(c => c.LastName, SortDirection.Ascending);

ThenBy(specification, sortDirection)

IOrderedQueryable and IOrderedEnumerable can use the method ThenBy(specification, SortDirection). This way it's easy to determine the sort direction of a list without the need of an if and ThenBy or ThenByDescending logic.

var result = TestData.List
             .OrderBy(c => c.LastName.Substring(0, 1), SortDirection.Descending)
             .ThenBy(c => c.LastName.Substring(1, 1), SortDirection.Descending);

Paging(pageSize, numberOfPagesToSkip)

Paging implementation on IQueryable and IEnumerable.

var pageSize = 2;
var numberOfPagesToSkip = 1;
var result = TestData.List.Paging(pageSize, numberOfPagesToSkip);

Specifications

Linq specifications are made for 2 reasons. The first reason is to guard a developer of writing the same expression every time he needs it. The second is making basic operations possible on linq expressions (and, or, not).

Basic operations

Multiple expressions can now be manipulated with some basic operations (or, and, not):

Expression<Func<Sample, bool>> specFirstName = x => x.FirstName.StartsWith("M");
Expression<Func<Sample, bool>> specLastName = x => x.LastName.StartsWith("M");

var orExpression = specFirstName.Or(specLastName);
var andExpression = specFirstName.And(specLastName)
var notExpression = ExpressionExtensions.Not(specFirstName);

var orResult = Data.Where(orExpression);
// orResult will contain "Manuel Rivera" and "Julian Mendez"

var andResult = Data.Where(andExpression);
// andResult will contain zero results

var notResult = Data.Where(notExpression);
// notResult will contain "Jose Rodriguez" and "Julian Mendez"

Custom specifications

If a specific Expression is used over and over, it is possible to contain it within a custom expression:

public class FirstNameSpecification : Specification<Sample>
{
    private string _firstName;
    public FirstNameSpecification(string firstName)
    { 
        _firstName = firstName;
    }

    public override Expression<Func<Sample, bool>> IsSatisfied()
    {
        return c => c.FirstName == _firstName;
    }
}

Use:

var spec = new FirstNameSpecification("Julian");
var result = Data.Where(spec.IsSatisfied());

AdHoc Specification

Creating a custom specification can be made AdHoc:

var spec = new AdHocSpecification<Sample>(x => x.FirstName.StartsWith("J"));
var result = Data.Where(spec.IsSatisfied());

Selector Specification

A Selector Specification exists so easy criteria can be made, controllered by the UI for example

var spec = new SelectorSpecification<Sample, string>(x => x.FirstName, Operation.StartsWith, "J");
var result = Data.Where(spec.IsSatisfied());

Specification Operations

Specifications can also be controlled via operations:

var specFirstName = new AdHocSpecification<Sample>(x => x.FirstName.StartsWith("J"));
var specLastName = new AdHocSpecification<Sample>(x => x.LastName.StartsWith("M"));

var specAnd = specFirstName && specLastName;
var specOr = specFirstName || specLastName;
var specNot = !specFirstName;

This could come handy in cases where input in the UI determines what filters need to be used. Just build up the specification according to the input parameters.

<< Back to main page

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 net452 is compatible.  net46 was computed.  net461 is compatible.  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.
  • .NETFramework 4.5.2

    • No dependencies.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

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
1.0.3 924 9/24/2018
1.0.2 991 3/3/2018
1.0.1 951 2/25/2018
1.0.0 992 2/24/2018