All Projects → Baune8D → LinqBuilder

Baune8D / LinqBuilder

Licence: MIT license
LinqBuilder is an advanced implementation of the specification pattern specifically targeting LINQ query generation.

Programming Languages

C#
18002 projects
powershell
5483 projects
shell
77523 projects
Batchfile
5799 projects

Projects that are alternatives of or similar to LinqBuilder

pnpcore
The PnP Core SDK is a modern .NET SDK designed to work for Microsoft 365. It provides a unified object model for working with SharePoint Online and Teams which is agnostic to the underlying API's being called
Stars: ✭ 169 (+397.06%)
Mutual labels:  dotnet-standard, dotnet5, dotnet6
Gridify
Easy and optimized way to apply Filtering, Sorting, and Pagination using text-based data.
Stars: ✭ 372 (+994.12%)
Mutual labels:  linq, entity-framework, entity-framework-core
Linq2db.entityframeworkcore
Bring power of Linq To DB to Entity Framework Core projects
Stars: ✭ 166 (+388.24%)
Mutual labels:  linq, entity-framework, entity-framework-core
N-Tier-Architecture
This is a n-layer architecture based on Common web application architectures.
Stars: ✭ 105 (+208.82%)
Mutual labels:  entity-framework-core, dotnet5, dotnet6
dotnet-learning
Материалы для обучения C# и ASP.NET
Stars: ✭ 62 (+82.35%)
Mutual labels:  linq, entity-framework-core
LinqSpecs
A toolset for use the specification pattern in LINQ queries.
Stars: ✭ 161 (+373.53%)
Mutual labels:  linq, specification-pattern
Nein Linq
NeinLinq provides helpful extensions for using LINQ providers such as Entity Framework that support only a minor subset of .NET functions, reusing functions, rewriting queries, even making them null-safe, and building dynamic queries using translatable predicates and selectors.
Stars: ✭ 347 (+920.59%)
Mutual labels:  linq, entity-framework
System.linq.dynamic.core
The .NET Standard / .NET Core version from the System Linq Dynamic functionality.
Stars: ✭ 864 (+2441.18%)
Mutual labels:  linq, entity-framework
Efcoresecondlevelcacheinterceptor
EF Core Second Level Cache Interceptor
Stars: ✭ 227 (+567.65%)
Mutual labels:  entity-framework, entity-framework-core
Linqtotwitter
LINQ Provider for the Twitter API (C# Twitter Library)
Stars: ✭ 401 (+1079.41%)
Mutual labels:  linq, dotnet-standard
Linq.translations
Declare properties on an object that can be translated by LINQ
Stars: ✭ 51 (+50%)
Mutual labels:  linq, entity-framework
EntityFrameworkCore.AutoFixture
A library aimed to minimize the boilerplate required to unit-test Entity Framework Core code using AutoFixture and in-memory providers.
Stars: ✭ 31 (-8.82%)
Mutual labels:  entity-framework, entity-framework-core
Csharp Datatables Parser
C# Serverside parser for the popuplar jQuery datatables plugin.
Stars: ✭ 119 (+250%)
Mutual labels:  linq, entity-framework-core
Remote.linq
Simply LINQ your remote resources...
Stars: ✭ 136 (+300%)
Mutual labels:  linq, entity-framework
Beetle.js
🪲 Javascript ORM, manage your data easily.
Stars: ✭ 53 (+55.88%)
Mutual labels:  linq, entity-framework
EFSqlTranslator
A standalone linq to sql translator that can be used with EF and Dapper.
Stars: ✭ 51 (+50%)
Mutual labels:  linq, entity-framework
Netfabric.hyperlinq
High performance LINQ implementation with minimal heap allocations. Supports enumerables, async enumerables, arrays and Span<T>.
Stars: ✭ 479 (+1308.82%)
Mutual labels:  linq, dotnet-standard
vscode-npm-gui
vscode nuget package manager gui https://marketplace.visualstudio.com/items?itemName=aliasadidev.nugetpackagemanagergui
Stars: ✭ 36 (+5.88%)
Mutual labels:  dotnet5, dotnet6
Onion Architecture Asp.net Core
WhiteApp API solution template which is built on Onion Architecture with all essential feature using .NET 5!
Stars: ✭ 196 (+476.47%)
Mutual labels:  entity-framework, entity-framework-core
Entityframework Extensions
Entity Framework Bulk Operations | Improve Entity Framework performance with Bulk SaveChanges, Insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL and SQLite.
Stars: ✭ 215 (+532.35%)
Mutual labels:  entity-framework, dotnet-standard

LinqBuilder

Build status codecov NuGet Badge

Available on NuGet: https://www.nuget.org/packages/LinqBuilder/
MyGet development feed: https://www.myget.org/F/baunegaard/api/v3/index.json

LinqBuilder is based on the specification pattern.

Table of Contents

  1. LinqBuilder Specifications
  2. LinqBuilder OrderSpecifications
  3. LinqBuilder.EFCore / LinqBuilder.EF6
  4. Full Example

LinqBuilder Specifications

Usage

Specifications can be constructed in three different ways.

By extending Specification:

public class FirstnameIsFoo : Specification<Person>
{
    public override Expression<Func<Person, bool>> AsExpression()
    {
        return person => person.Firstname == "Foo";
    }
}

ISpecification<Person> firstnameIsFoo = new FirstnameIsFoo();

By extending DynamicSpecification:

public class FirstnameIs : DynamicSpecification<Person, string>
{
    public override Expression<Func<Person, bool>> AsExpression()
    {
        return person => person.Firstname == Value;
    }
}

ISpecification<Person> firstnameIsFoo = new FirstnameIs().Set("Foo");

By extending MultiSpecification:

public class FirstnameIsFoo : MultiSpecification<Person, OtherPerson>
{
    public override Expression<Func<Person, bool>> AsExpressionForEntity1()
    {
        return person => person.Firstname == "Foo";
    }

    public override Expression<Func<OtherPerson, bool>> AsExpressionForEntity2()
    {
        return person => person.Firstname == "Foo";
    }
}

ISpecification<Person> firstnameIsFoo = new FirstnameIsFoo(); // First generic is default
ISpecification<Person> firstnameIsFoo = new FirstnameIsFoo().For<Person>();
ISpecification<OtherPerson> firstnameIsFoo = new FirstnameIsFoo().For<OtherPerson>();

By static New method:

ISpecification<Person> firstnameIsFoo = Specification<Person>.New(p => p.Firstname == "Foo");
// Or by alias
ISpecification<Person> firstnameIsFoo = Spec<Person>.New(p => p.Firstname == "Foo");

Example

var collection = new List<Person>() { ... };

ISpecification<Person> firstnameIsFoo = Spec<Person>.New(p => p.Firstname == "Foo");
ISpecification<Person> firstnameIsBar = Spec<Person>.New(p => p.Firstname == "Bar");

ISpecification<Entity> specification = firstnameIsFoo.Or(firstnameIsBar);

var result = collection.ExeSpec(specification).ToList();
// result = Collection items satisfied by specification

The extension ExeSpec allows all types of ISpecification to be executed on IQueryable and IEnumerable.

Methods

ISpecification<Entity> specification = Spec<Entity>.All(
    new SomeSpecification(),
    new SomeOtherSpecification(),
    ...
);

ISpecification<Entity> specification = Spec<Entity>.None(
    new SomeSpecification(),
    new SomeOtherSpecification(),
    ...
);

ISpecification<Entity> specification = Spec<Entity>.Any(
    new SomeSpecification(),
    new SomeOtherSpecification(),
    ...
);

Extensions

ISpecification<TEntity> And<TEntity>(this ISpecification<TEntity> left, ISpecification<TEntity> right);
ISpecification<TEntity> Or<TEntity>(this ISpecification<TEntity> left, ISpecification<TEntity> right);
ISpecification<TEntity> Not<TEntity>(this ISpecification<TEntity> specification);
bool IsSatisfiedBy<TEntity>(this ISpecification<TEntity> specification, TEntity entity);
ISpecification<TEntity> Clone<TEntity>(this ISpecification<TEntity> specification);

LinqBuilder also extends the following extensions to support ISpecification on IQueryable and IEnumerable.

IEnumerable<Entity> collection = collection.Where(specification);
bool result = collection.Any(specification);
bool result = collection.All(specification);
int result = collection.Count(specification);
Entity result = collection.First(specification);
Entity result = collection.FirstOrDefault(specification);
Entity result = collection.Single(specification);
Entity result = collection.SingleOrDefault(specification);

LinqBuilder OrderSpecifications

Usage

Order specifications can be constructed in almost the same way as regular specifications.

By extending OrderSpecification:

public class FirstnameDescending : OrderSpecification<Person, string>
{
    public DescNumberOrderSpecification() : base(Sort.Descending) { }

    public override Expression<Func<Person, string>> AsExpression()
    {
        return person => person.Firstname;
    }
}

ISpecification<Person> firstnameDescending = new FirstnameDescending();

By static New method:

ISpecification<Person> firstnameDescending = OrderSpecification<Person, string>.New(p => p.Firstname, Sort.Descending);
// Or by alias
ISpecification<Person> firstnameDescending = OrderSpec<Person, string>.New(p => p.Firstname, Sort.Descending);

Example

var collection = new List<Person>() { ... };

ISpecification<Person> firstnameDescending = OrderSpec<Person, string>.New(p => p.Firstname, Sort.Descending);
ISpecification<Person> lastnameDescending = OrderSpec<Person, string>.New(p => p.Lastname, Sort.Descending);

ISpecification<Person> specification = firstnameDescending.ThenBy(lastnameDescending);

var result = collection.ExeSpec(specification).ToList();
// result = Collection ordered by descending number, then by other number

Methods

ISpecification<Person> specification = OrderSpec<Person, string>.New(p => p.Firstname)
    .Take(10);

ISpecification<Person> specification = OrderSpec<Person, string>.New(p => p.Firstname)
    .Skip(5);

ISpecification<Person> specification = OrderSpec<Person, string>.New(p => p.Firstname)
    .Paginate(2, 10); // Equals .Skip((2 - 1) * 10).Take(10)

Extensions

IOrderedEnumerable<Entity> collection = collection
    .OrderBy(specification);
    .ThenBy(otherSpecification);

Order specifications can also be chained with regular LinqBuilder specifications.

ISpecification<Person> firstnameIsFoo = Spec<Person>.New(p => p.Firstname == "Foo");
ISpecification<Person> firstnameAscending = OrderSpec<Person, string>.New(p => p.Firstname);

ISpecification<Entity> specification = firstnameIsFoo.OrderBy(firstnameAscending);

Chained OrderSpecification's can also be attatched to a specification later.

ISpecification<Person> firstnameIsFoo = Spec<Person>.New(p => p.Firstname == "Foo");
ISpecification<Person> firstnameAscending = OrderSpec<Person, string>.New(p => p.Firstname);
ISpecification<Person> lastnameAscending = OrderSpec<Person, string>.New(p => p.Firstname);

ISpecification<Person> orderSpecification = firstnameAscending.ThenBy(lastnameAscending);

ISpecification<Person> specification = firstnameIsFoo.UseOrdering(orderSpecification);

The following extensions will help to check what kind of ordering is applied.

ISpecification<Person> firstnameIsFoo = Spec<Person>.New(p => p.Firstname == "Foo");
ISpecification<Person> firstnameAscending = OrderSpec<Person, string>.New(p => p.Firstname);

firstnameIsFoo.IsOrdered(); // Returns false

ISpecification<Person> specification = firstnameIsFoo.OrderBy(firstnameAscending);
specification.IsOrdered(); // Returns true
ISpecification<Person> specification = Spec<Person>.New(p => p.Firstname == "Foo");
specification.HasSkip(); // Returns false

ISpecification<Person> specification = specification.Skip(10);
specification.HasSkip(); // Returns true
ISpecification<Person> specification = Spec<Person>.New(p => p.Firstname == "Foo");
specification.HasTake(); // Returns false

ISpecification<Person> specification = specification.Take(10);
specification.HasTake(); // Returns true

LinqBuilder.EFCore / LinqBuilder.EF6

Package Version
LinqBuilder.EFCore NuGet Badge
LinqBuilder.EF6 NuGet Badge

Extensions

LinqBuilder.EF packages extends the following extensions to support ISpecification.

bool result = await _sampleContext.Entities.AnyAsync(specification);
bool result = await _sampleContext.Entities.AllAsync(specification);
int result = await _sampleContext.Entities.CountAsync(specification);
Entity result = await _sampleContext.Entities.FirstAsync(specification);
Entity result = await _sampleContext.Entities.FirstOrDefaultAsync(specification);
Entity result = await _sampleContext.Entities.SingleAsync(specification);
Entity result = await _sampleContext.Entities.SingleOrDefaultAsync(specification);

Full example

public class Person
{
    public int Id { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}

public class SampleDbContext : DbContext // Simplified DbContext
{
    public virtual DbSet<Person> Persons { get; set; }
}

public class DbService<TEntity> where TEntity : class
{
    private readonly DbSet<TEntity> _dbSet;

    public DbService(SampleDbContext context)
    {
        _dbSet = context.Set<TEntity>();
    }

    public int Count(ISpecification<TEntity> specification)
    {
        return _dbSet.Count(specification);
    }

    public List<Entity> Get(ISpecification<TEntity> specification)
    {
        return _dbSet.ExeSpec(specification).ToList();
    }

    public (List<Person> items, int count) GetAndCount(ISpecification<TEntity> specification)
    {
        return (Get(specification), Count(specification));
    }
}

ISpecification<Person> firstnameIsFoo = Spec<Person>.New(p => p.Firstname == "Foo");
ISpecification<Person> lastnameIsBar = Spec<Person>.New(p => p.Lastname == "Bar");
ISpecification<Person> idDescending = OrderSpec<Person, int>.New(p => p.Id, Sort.Descending);

ISpecification<Person> specification = firstnameIsFoo.And(lastnameIsBar)
    .OrderBy(idDescending)
    .Paginate(1, 5); // pageNo = 1, pageSize = 5

using (var context = new SampleDbContext())
{
    var result = new DbService<Person>(context).GetAndCount(specification);
    // result.items = Paginated list of Person's with name: Foo Bar
    // result.count = Total unpaginated result count
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].