Project Description

LinqTextQueryBuilder makes it easier for developers to build/run dynamic LINQ queries from text at runtime.

Articles

Dynamic LINQ Queries (Build LINQ queries from text at runtime)


Example

You can use LinqTextQueryBuilder with any LINQ collection

List<int> intList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
var source = intList.AsQueryable();

LinqTextQueryBuilder queryBuilder = new LinqTextQueryBuilder();

queryBuilder.SetSource( source );
string queryText = @"from int i in source
where i > 4
select i";
var result = queryBuilder.Query( queryText );
foreach ( var item in result )
{
Console.WriteLine( item );
}


Now a little bit complicated example

public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public string Company { get; set; }
public string Position { get; set; }

public override string ToString()
{
return string.Format( "{0} {1}", Name, Age );
}
}


List<Employee> personList = new List<Employee>(){
new Employee(){ Name="Steve", Age =23, Position="Developer"},
new Employee(){ Name="Mark", Age =32, Position="Designer"},
new Employee(){ Name="Bill", Age =23, Position="Developer"},
new Employee(){ Name="Nill", Age =25, Position="Analyst"},
new Employee(){ Name="Kevin", Age =28, Position="Analyst"},
new Employee(){ Name="Steve", Age =22, Position="Designer"}
};
var source = personList.AsQueryable();

LinqTextQueryBuilder queryBuilder = new LinqTextQueryBuilder();
queryBuilder.Profiler.AddNamespace( "LinqTextQueryBuilderSample" );
queryBuilder.SetSource( source );
string queryText = "from Employee e in source\n" +
"where e.Position == \"Developer\"\n" +
"select e\n";
var result = queryBuilder.Query( queryText );
foreach ( var item in result )
{
Console.WriteLine( item );
}	


In this example we use list of Employee class as source also we add our Employee class' namespace to LinqTextQueryBuilder profiler.

queryBuilder.Profiler.AddNamespace( "LinqTextQueryBuilderSample" );


Also we can add some reference libraries using AddAssembly method. But in this situation we do not need add. The reason is Employee class is in current Assembly and LinqTextQueryBuilder automaticaly add all references of current Assembly to its own references.

Also you can use Anonymous Types

string queryText = "from Employee e in source\n" +
			"where e.Age > 22\n" +
			"select new { e.Name, e.Position }\n";


Another advantage of LinqTextQueryBuilder is using generic types so you can use different types as source such as DataContext (LINQ To SQL) or Entity Framework Data Context. Example of usage LinqTextQueryBuilder with LINQ To SQL (in example of Northwind Database)

NorthwindDataContext db = new NorthwindDataContext();

LinqTextQueryBuilder<NorthwindDataContext> queryBuilder = new LinqTextQueryBuilder<NorthwindDataContext>();
queryBuilder.Profiler.AddNamespace( "NorthwindDataContext" );
queryBuilder.SetSource( db );
string queryText = "from Product p in source\n" +
"where p.Category.CategoryName == \"Beverages\"\n" +
"select p\n";
var result = queryBuilder.Query( queryText );
foreach ( var item in result )
{
Console.WriteLine( item );
}

 


You can test all features of LinqTextQueryBuilder using test application

LinqTestQueryBuilder Test Application

Last edited Oct 7, 2011 at 5:11 PM by KenanBek, version 5