Pargoon.Extensions.Linq 1.0.0

Suggested Alternatives

Pargoon.Extensions.Linq 1.0.2

Additional Details

deprecated

There is a newer version of this package available.
See the version list below for details.
dotnet add package Pargoon.Extensions.Linq --version 1.0.0                
NuGet\Install-Package Pargoon.Extensions.Linq -Version 1.0.0                
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="Pargoon.Extensions.Linq" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pargoon.Extensions.Linq --version 1.0.0                
#r "nuget: Pargoon.Extensions.Linq, 1.0.0"                
#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 Pargoon.Extensions.Linq as a Cake Addin
#addin nuget:?package=Pargoon.Extensions.Linq&version=1.0.0

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

Pargoon.Extensions.Linq

Overview

Pargoon.Extensions.Linq is a C# library that provides a set of extension methods for enhancing IQueryable<T> and IEnumerable<T> operations. These extensions focus on conditional filtering (WhereIf) and dynamic sorting (Sorting), allowing developers to build more flexible and maintainable LINQ queries.

Features

  • Conditional Filtering: Easily apply filters to queries based on runtime conditions.
  • Dynamic Sorting: Sort collections dynamically based on property names and sort directions, with support for multiple sorting criteria.
  • Supports Both IQueryable<T> and IEnumerable<T>: Although the library is optimized for IQueryable<T>, it also provides fallback support for IEnumerable<T> by converting to IQueryable<T>.

Installation

To use the Pargoon.Extensions.Linq library, simply include the source file in your project or compile the code into a DLL and reference it in your project.

Usage

1. Conditional Filtering (WhereIf)

The WhereIf extension method allows you to apply a filter to a query only if a specified condition is true.

using Pargoon.Extensions.Linq;

// Applying a filter conditionally
var filteredData = data.WhereIf(isActive, x => x.IsActive);
  • isActive: A boolean condition.
  • x => x.IsActive: The predicate to apply if isActive is true.

2. Dynamic Sorting (Sorting)

The Sorting extension methods provide a way to sort collections based on property names and sort directions at runtime.

Single Property Sorting
using Pargoon.Extensions.Linq;

var sortedData = data.Sorting("PropertyName", SortDirection.Asc);
  • PropertyName: The name of the property to sort by.
  • SortDirection.Asc: Sort direction (ascending or descending).
Multiple Property Sorting
using Pargoon.Extensions.Linq;

var sortItems = new List<SortItem>
{
    new SortItem { PropertyName = "FirstName", Direction = SortDirection.Asc },
    new SortItem { PropertyName = "LastName", Direction = SortDirection.Desc }
};

var sortedData = data.Sorting(sortItems);
  • sortItems: A list of SortItem objects specifying the properties and directions to sort by.

3. Custom Comparers

If you need to use a custom comparer for sorting, you can pass it as an optional parameter:

using Pargoon.Extensions.Linq;

var sortedData = data.Sorting("PropertyName", SortDirection.Asc, comparer: new CustomComparer());
  • CustomComparer: Your custom IComparer<object> implementation.

Example

Here's a complete example of using Pargoon.Extensions.Linq in a typical scenario:

using Pargoon.Extensions.Linq;

var isActive = true;
var sortItems = new List<SortItem>
{
    new SortItem { PropertyName = "FirstName", Direction = SortDirection.Asc },
    new SortItem { PropertyName = "LastName", Direction = SortDirection.Desc }
};

var query = dbContext.Users
    .WhereIf(isActive, x => x.IsActive)
    .Sorting(sortItems);

var result = query.ToList();

In this example:

  • Users are filtered by IsActive only if isActive is true.
  • The filtered results are then sorted by FirstName in ascending order and LastName in descending order.

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.

License

This library is licensed under the MIT License. See the LICENSE file for more details.


With Pargoon.Extensions.Linq, you can build more dynamic and flexible LINQ queries, improving both the readability and maintainability of your code.

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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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 (1)

Showing the top 1 NuGet packages that depend on Pargoon.Extensions.Linq:

Package Downloads
Noyan.ShadMessage.ApiContracts

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.