expression-trees

Bit Curious to understand Expression Tree in .NET

泪湿孤枕 提交于 2019-12-06 02:15:30
问题 I have read several articles and several stackoverflow.com posts about expression tree. It is beating my brain to understand. Questions: 1) Like DOM (Document Object Model), it is an in-memory representation of logic? 2) Somebody explained it is a mechanism to translate an executable code into data, using it we can produce a data structure representing the code. Does it mean, expression trees are used to design a user defined pattern? 3) Most of the Examples show Expression tree in

Unable to create a compound Expression<Func<string, bool>> from a set of expressions

徘徊边缘 提交于 2019-12-06 00:45:20
(Answer towards the bottom) I'm trying to build a system that combines Func<T, bool> delegates into an ExpressionTree that allows me to pass in a value (badValue in this case) and get a bool out if the predicates all return true and the binary operations are taken into account. This is my first time using Expression/ExpressionTrees, so please be gentle. I'm getting this error: ArgumentException: Expression of type 'System.Boolean' cannot be invoked on this line: collectAnswers = Expression.And(isEmpty.Body, Expression.Invoke(... I've got that line set up that way because I need to share the

Why is a conversion necessary in Expression Trees

可紊 提交于 2019-12-06 00:02:09
问题 From this question I asked 5 minutes ago, it's clear that the following code throws an exception, stating that Unhandled Exception: System.InvalidOperationException: The binary operator Equal is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Int32'. Code public static void GetResultCollection<T>() { AccrualTrackingEntities db = new AccrualTrackingEntities(); var result = db.CreateQuery<T>(String.Format("[{0}]", typeof(T).Name + "s")); int? ItemTypeValue = 1; var param

How does one create a .NET Expression with NodeType of ExpressionType.Index?

[亡魂溺海] 提交于 2019-12-05 18:41:00
I'm writing code that evaluates .NET Expression trees. I'm trying to create a C# 4 test to exercise my handling of an ExpressionType.Index , but I can't figure out how to create that type of expression through a LambdaExpression . No matter what I try, the expression comes out as an ExpressionType.Call or ExpressionType.ArrayIndex . For example: IList<int> myList = new ObservableCollection<int> { 3, 56, 8 }; Expression<Func<int>> myExpression = () => myList[3]; // myExpression.Body.NodeType == ExpressionType.Call myList = new int[] { 3, 56, 8 }; myExpression = () => myList[3]; // myExpression

force Expression<> to evaluate local variables

可紊 提交于 2019-12-05 17:59:36
I have something like this in LinqPad void Main() { var t1 = DateTimeOffset.Parse("10/1/2012"); int? n1 = 1; Expression<Func<Sample,bool>> x1 = ud => (ud.Date == t1 && ud.Number == n1); x1.ToString().Dump(); } class Sample { public int? Number{set;get;} public DateTimeOffset Date{set;get;} } it outputs ud => ((ud.Date == value(UserQuery+<>c_ DisplayClass0).t1) AndAlso (ud.Number == value(UserQuery+<>c _DisplayClass0).n1)) is there any possible way to keep the variables but have it output something like this: ud => ((ud.Date == Parse("10/1/2012")) AndAlso (ud.Number == Convert(1))) Here we go;

a simple way to generate SQL Server's “standard” form of an expression?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 17:57:27
This relates to computed columns and default constraints (and possibly other expressions) in SQL Server 2005 (or above). Both of these use an arbitrary expression to generate a value, e.g. (year+1) for a computed column representing "next year" (this is obviously a simple and stupid example). What I'm trying to do: I want to be able to determine whether an existing computed column (or default constraint) in some table matches the intended definition, where the latter is defined in a software-generated schema that was used to create the table. I can get the definition of a computed column using

Expression Tree with Property Inheritance causes an argument exception

馋奶兔 提交于 2019-12-05 17:49:57
Following this post: link text I'm trying to create an expression tree that references the property of a property. My code looks like this: public interface IFoo { void X {get;set;} } public interface IBar : IFoo { void Y {get;set;} } public interface IFooBarContainer { IBar Bar {get;set;} } public class Filterer { //Where T = "IFooBarContainer" public IQueryable<T> Filter<T>(IEnumerable<T> collection) { var argument = Expression.Parameter(typeof (T), "item"); //... //where propertyName = "IBar.X"; PropertyOfProperty(argument, propertyName); } private static MemberExpression PropertyOfProperty

Compile expression that requires a parameter

我与影子孤独终老i 提交于 2019-12-05 17:30:15
Ok, I am sure this is simple, but I am having a senior moment. I have a simple BinaryExpression (greaterthan) the left side is a ParameterExpression and the right side is a ConstantExpression I want to compile this expression to a func that I can call and pass a parameter to... var func = ...something with my exp.... bool result = func(myValue); Thanks to Hasan, I modified his answer to my needs... var func = Expression.Lambda<Func<int,bool>>(myExpr, (ParameterExpression)myExpr.left).Compile(); var param = Expression.Parameter(typeof(int)); var value = Expression.Constant(3); var body =

Implement Not in expression trees, .net 4

谁都会走 提交于 2019-12-05 16:23:37
Is it possible to implement a ! (not) using expression trees. I'm interested in creating a C# eval class which will parse and evaluate logical expressions which contain true, false, ||, && and !. I know that && and || are currently supported by .NET 4 expression trees, but I was wondering if their is a way to implement summat like !(x && y) || z where z=false, y=true and z=false. Currently I'm using a standard stack based tokenizer, parser, evaluator to evaluate these types of expression but would happly dump it, if a suitable expression tree could be created and executed on the fly. I usually

Can I evaluate an expression in a way to determine and possibly set a property that is null?

青春壹個敷衍的年華 提交于 2019-12-05 13:53:58
I have a service that takes an object and based on the properties within will perform different actions; with this any of these properties can be null, meaning don't perform this action. I am trying to create a very simple to use API to do this in cases where some properties can be multiple levels deep, here is an example of the current implementation service.PerformActions(DataFactory.GetNewData<ActionsInfo> ( data => data.SomeParent = DataFactory.GetNewData<SomeParentInfo>(), data => data.SomeParent.SomeProperty = "someValue" )); This is a slightly simplified version and in real cases I some