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

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?

Where can I find online examples?

Online examples are now available!

Online Examples
Is there other similar libraries?

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

We need your help to support this Dynamic LINQ!

Dynamic LINQ is FREE and always will be.

However, last year alone, we spent over 3000 hours maintaining our free projects! We need resources to keep developing our open-source projects.

We highly appreciate any contribution!


> 3,000+ Requests answered per year
> $100,000 USD investment per year
> 500 Commits per year
> 100 Releases per year


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);