Dynamic LINQ A FREE & Open Source LINQ Dynamic Query Library


Downloaded more than
0
times !
using (var context = new EntityContext())
{
   var list = customers
      .Where("Name.Contains(@0)","ZZZ Projects")
      .ToList();
}



What's Dynamic LINQ

The Dynamic LINQ library let you execute query with dynamic string and provide some utilities methods such as ParseLambda, Parse, and CreateClass.



Where can I find online examples?

Online examples are now available!

Online Examples


Is there other similar libraries?

If you need more flexibility, we recommend you our C# Eval Expression library which is more powerful and let you Evaluate, Compile and Execute any C# code at runtime.

C# Eval Expression

Our achievements


5000+ Customers
75+ Countries
8000+ Requests closed
50 000 000+downloads
4 000 000+daily visitors

What we achieved over the last 5 years has grown beyond our hopes. That motivates us to continue to grow and improve all our projects. Every day, we are committed to listening to our clients to help ease the daily dev workload as much as possible.


Dynamic Extension Methods

Execute LINQ Query with string

  • Dynamic Group By
  • Dynamic Select
  • Dynamic Where Clause
using (var context = new EntityContext())
{
    var query =  context.Customers
        .Where("Orders.Count >= @0", 5)
        .OrderBy("Orders.Count")
        .ToList();
}

Parse Lambda Method

TheSystem.Linq.Dynamic.Core.DynamicExpressionParser class defines the ParseLambda method for dynamically parsing and creating lambda expressions.

using (var context = new EntityContext())
{
    Expression<Func<Customer, bool>> e1 = DynamicExpressionParser
        .ParseLambda<Customer, bool>(new ParsingConfig(), true, "City = @0", "London");
   
    Expression<Func<Customer, bool>> e2 = DynamicExpressionParser
        .ParseLambda<Customer, bool>(new ParsingConfig(), true, "Orders.Count >= 3");

    var customers = context.Customers.Where("@0(it) and @1(it)", e1, e2).ToList();
}

Parse Method

The Parse method is used for parsing and creating expression tree fragments that are defined in System.Linq.Dynamic.Core.Parser.ExpressionParser class. It uses the TextParser to parse the string into the specified result type.

ParameterExpression x = Expression.Parameter(typeof(int), "x");
ParameterExpression y = Expression.Parameter(typeof(int), "y");

var symbols = new[] { x, y };

Expression body = new ExpressionParser(symbols, "(x + y) * 2", symbols, new ParsingConfig()).Parse(typeof(int));

LambdaExpression e = Expression.Lambda(body, new ParameterExpression[] { x, y });

Create Data Class Dynamically

A data class is a class that contains only data members. The System.Linq.Dynamic.DynamicExpression class defines the following methods for dynamically creating data classes.

DynamicProperty[] props = new DynamicProperty[]
{
    new DynamicProperty("Name", typeof(string)),
    new DynamicProperty("Birthday", typeof(DateTime))
};

Type type = System.Linq.Dynamic.DynamicExpression.CreateClass(props);

object obj = Activator.CreateInstance(type);

type.GetProperty("Name").SetValue(obj, "Albert", null);
type.GetProperty("Birthday").SetValue(obj, new DateTime(1879, 3, 14), null);

Console.WriteLine(obj);