Pargoon.Extensions.Linq 1.0.1

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.1                
NuGet\Install-Package Pargoon.Extensions.Linq -Version 1.0.1                
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.1" />                
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.1                
#r "nuget: Pargoon.Extensions.Linq, 1.0.1"                
#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.1

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

Pargoon.Extensions.Linq

Overview

Pargoon.Extensions.Linq is a C# library that enhances LINQ operations by providing a set of extension methods. These methods allow for conditional filtering and dynamic sorting of IQueryable<T> collections, making it easier to build flexible and maintainable queries.

Features

  • Conditional Filtering: Use WhereIf to apply filters only when a specified condition is true.
  • Dynamic Sorting: Sort collections based on property names and directions specified at runtime, with support for sorting on multiple properties.
  • Supports Custom Comparers: Sorting methods support custom comparers for advanced sorting scenarios.

Installation

To use the Pargoon.Extensions.Linq library, add the source file to your project or compile it 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 enable sorting 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: The direction to sort in (ascending or descending).
Multiple Property Sorting

You can sort by multiple properties by providing a list of SortItem objects.

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 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 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 dynamic and flexible LINQ queries that improve 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.