Going to use the following object as a basis for the examples
public class Ref_Test
    public int Id {get;set;}
    public string Txt {get;set;}

Main Pieces To The Library (everything is in the ToracTechnologies.ExpressionTreeLibrary.DynamicApi.Framework namespace)
  • ParameterBuilder Helps build up a property into ParameterBuilderResults. This would be if I want to use Ref_Test.Id to build onto
  • ExpressionBuilder This helps if you want to build something on that property. ==, >=, >, <, <=, !=, IEnumerableContains, StringContains, DynamicSelect.
  • ExpressionCombiner will help you combine expressions. Right now you can combine them with an "AndAlso" and a "OrElse"

Let's start with some scenario's
  • I want to dynamically say x.Id >= 3 and return that expression so I can use it in Linq or EF

First I need to get the Id property. If I have Id as a string I can call the following methods
  var createMyProperty = BuildParameterFromStringName<Ref_Test>("Id");

  var createMyProperty = BuildParameterFromStringName("Id", typeof(Ref_Test));

If i have a another lambda that points this property such as:
 Expression<Func<T, Int32>> myIdProperty = x => x.Id;

then I can call:
var createMyProperty = BuildParameterFromLinqPropertySelector(myIdProperty);

I now have my parameter by using 1 of the 3 ways to build a property.
I can now call ToracTechnologies.ExpressionTreeLibrary.DynamicApi.Framework.BuildStatement
 var myExpressionToUse = ExpressionBuilder.BuildStatement<Ref_Test, int>(createMyProperty, ExpressionBuilder.DynamicUtilitiesEquations.GreaterThanOrEqual, 3);

I can use that expression to query EF or Linq. If you are using it in linq just call myExpressionToUse.Compile().

var compiledExpression = myExpressionToUse.Compile(); 
var queryResults = _myArray.Where(compiledExpression);

  • Let's say I want to add an "Or" statement where x.Id >= 3 or x.Id == 2

(going to skip building up the x.Id == 2 because the syntax is the same as above only you would use ExpressionBuilder.DynamicUtilitiesEquations.Equal)

To combine expressions use the following
var newExpression = ToracTechnologies.ExpressionTreeLibrary.DynamicApi.Framework.CombineExpression(myExpressionToUse, ExpressionCombiner.CombineType.OrElse,myIdEqual2Expression);

You can now use the newExpression to query for the results
  • Let's say I want to build up a dynamic select statement with a projection
    Expression<Func<Ref_Test, SomeNewObject>> thisExp = x => new SomeNewObject
                    txt = x.Description

Now I want to set Id to some dynamic field based on an if statement. I can merge 2 expressions that are selects. The Before / After determines if you want to put the Id before the Txt or after.
  if (thisIsTrue)
   thisExp = thisExp.Merge(x => new TempHolder
                         id = x.Id
                     }, ExpressionRemapperShared.ExpressionMemberInitMergerPosition.Before);

  • Let's say I want to build up a dynamic select statement and merge a sub object I can use merge sub object. The 2 parameter is the property of the sub item you want to merge
baseExp.MergeSubObject(mergedSubItems, x => x.SubItem, ExpressionRemapperShared.ExpressionMemberInitMergerPosition.After);

There are several utilities in the LinqUtilities / Entity Framework Utilities
  • PaginateResults
  • OrderByLinqStatement (sort by a property name where you only have a string name)
  • StringContains

In Expression Builder There is also the following
  • BuildIEnumerableContains
  • Select

Extension Methods
  • Merge and MergeSubObject are in ToracTechnologies.ExpressionTreeLibrary.ExtensionMethods.ExpressionTrees.LinqExpressionExtMethods
  • PaginateResults and order by are in ToracTechnologies.ExpressionTreeLibrary.ExtensionMethods.IQueryable.IQueryableExtMethods

Last edited Aug 29, 2014 at 12:20 PM by Torac24, version 16