Skip to content

Latest commit

 

History

History
93 lines (74 loc) · 2.76 KB

readme.md

File metadata and controls

93 lines (74 loc) · 2.76 KB

ExpresionBuiler readme

A fast way to create Linq expressions

This project allows you to create a list of expressions, and compile the to a single AND / OR linq query. very usefall when working whit dynamic queries.

Examples

First create a list of expressions, each expressions is comprised of a Func of an object, UserModel in the exmpales below, and bool.

var andList = new List<Expression<Func<UserModel,bool>>>();
andList.Add((o) => o.Last != "smith" && o.Age > 5);
andList.Add((o) => o.Age > 5);
andList.Add((o) => o.isTemp);
andList.Add((o) => o.Rank == 1);

var orList = new List<Expression<Func<UserModel,bool>>>();
andList.Add((o) => o.Age !=12);
andList.Add((o) => o.Rank >2 1);

AND query

var query=ExpresionTreeBuilder.CreateANDQuery<UserModel>(andList).Compile();
var test = allUsers.Where(query);

Or create a compiled query.

var query=ExpresionTreeBuilder.CreateCompiledANDQuery<UserModel>(andList);
var test = allUsers.Where(query);

OR query

var query=ExpresionTreeBuilder.CreateORQuery<UserModel>(OrList).Compile();
var test = allUsers.Where(query);

Or create a compiled query.

var query=ExpresionTreeBuilder.CreateCompiledOrQuery<UserModel>(OrList);
var test = allUsers.Where(query);

Mixed AND and OR query

var query=ExpresionTreeBuilder.CreateQuery<UserModel>(andList,OrList).Compile();
var test = allUsers.Where(query);
var query=ExpresionTreeBuilder.CreateCompiledQuery<UserModel>(andList,OrList);
var test = allUsers.Where(query);

Properties sorting

Each of the above methods can recive an optional bool param named sortProperties, deafult value is True. When this param is set to True it will sort the order of the queries in the list to make sure the 'easiest' come first.

the order is:

  1. bool
  2. Integers (Int and long)
  3. Floating point (Float, Double, Decimal)
  4. string and Guid
  5. datetime
  6. other

Order query

You can also create order by queries.

var orderAscList = new List<Expression<Func<UserModel,dynamic>>>();
var orderDescList2 = new List<Expression<Func<UserModel,dynamic>>>();
orderAscList.Add((o) => o.Age);
orderDescList2.Add((o) => o.Rank);

Order by Asc

allUsers = ExpresionTreeBuilder.CreateOrderASCQuery<UserModel>(allUsers, orderAscList);

Order by Desc

allUsers = ExpresionTreeBuilder.CreateOrderDescQuery<UserModel>(allUsers, orderDescList2);

Order by Asc and Desc

allUsers = ExpresionTreeBuilder.CreateOrderQuery<UserModel>(allUsers,orderAscList,orderDescList2,false);