I am attempting to create a dynamic query using expression trees in LINQ to represent the following query
WageConstIns.Where(WageConstIn => WageConstIn.Serialno.ToString().StartsWith("2800"));
I have attempted to create it like so:
MemberExpression le1 = LinqExpression.Property(paramExp, "Serialno");
MethodCallExpression le2 = LinqExpression.Call(le1, typeof(string).GetMethod("ToString", System.Type.EmptyTypes));
ConstantExpression le3 = LinqExpression.Constant("2800");
MethodCallExpression le4 = LinqExpression.Call(le2, typeof(string).GetMethod("StartsWith"));
I am getting an error during runtime. How can the above query best be built using expression trees?
The easiest way would be to just declare it as an
Expression<Func<...>>But if you want to construct it using different Expressions…
Most people [that I’ve talked to] who enter the domain of expression trees are usually satisfied with the System.Linq.Dynamic functionality. (Which can be abused into a lot of different ways.) This code snippet of pure awesomeness is a part of the Visual Studio sample code, probably hiding somewhere on your harddrive already.