expression-trees

Expression with dynamic class

≯℡__Kan透↙ 提交于 2019-12-24 17:29:15
问题 I'm trying to make Expressions work with dynamic classes that inherits DynamicObject like this: // Dynamic class defintion public class DynamicClass1 : DynamicObject { // Code here... } // Here is where I try to create where "someproperty" is untyped and the DynamicClass1 is referenced (not shown here) public static IQueryable DoStuff(this IQueryable source, string predicate, params object[] values) { LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool),

Entity Framework - Linq - Unable to create a constant value of type ‘System.Object’. Only primitive types

偶尔善良 提交于 2019-12-24 09:48:55
问题 I have a method to build an expression for a linq query for a given type, property, and value. This works wonderfully as long as the property on the type is NOT nullable. Here is the example I am working from (http://www.marcuswhitworth.com/2009/12/dynamic-linq-with-expression-trees) I am calling the Equals method on the property. However I have discovered that the Equals method for Nullable types takes an Object as a parameter instead of the Nullable type. I attempted to use

How do I subscribe to an event of an object inside an expression tree?

二次信任 提交于 2019-12-24 06:31:08
问题 Sorry I couldn't think of a better title. This is a two part question that only make sense together. Say I have a constructor like this public Fact(INotifyPropertyChanged observable, Func<bool> predicate) { this.predicate = predicate; observable.PropertyChanged += (sender, args) => PropertyChanged(this, new PropertyChangedEventArgs("Value")); } and this is how it's used new Fact(Model.AllowEditing, () => Model.AllowEditing); where AllowEditing is a type of INotifyPropertyChanged I would like

Building Expression Tree Using a Parameter's Indexer

点点圈 提交于 2019-12-24 05:15:17
问题 Given a class that has a property that is a Dictionary public class Product { public Dictionary<string, string> Attributes { get { return attributes; } } private Dictionary<string, string> attributes = new Dictionary<string, string>(); } I want to be able to match products in a list of products based on criteria that are retrieved from a data store that are in the format of Brand == Tyco Color != Blue My current approach is to construct an expression from the filter, and then pass that

Comparing all properties of an object using expression trees

社会主义新天地 提交于 2019-12-24 02:13:13
问题 I'm trying to write a simple generator that uses an expression tree to dynamically generate a method that compares all properties of an instance of a type to the properties of another instance of that type. This works fine for most properties, like int an string , but fails for DateTime? (and presumably other nullable value types). The method: static Delegate GenerateComparer(Type type) { var left = Expression.Parameter(type, "left"); var right = Expression.Parameter(type, "right");

Converting expression to another?

与世无争的帅哥 提交于 2019-12-24 01:27:30
问题 I have a little challenge which I don't know how to solve. I need to convert from this Expression<Func<TEntity, URequest, bool>> to this Expression<Func<TEntity, bool>> . The 2nd is going to be used to query a data source. The idea is this having a base class constructor with the following signature abstract class Base { Base(Expression<Func<TEntity, TRequest, bool>> expression) { .... } } I can provide an expression in the derived something like this class Derived : Base { Derived() : base (

Expression trees - different compiler behaviour for Action<T> in C# and VB.NET

夙愿已清 提交于 2019-12-24 01:03:58
问题 Given a simple piece of code which can return the name of a property in VB.NET: Function NameForProperty(Of T)(ByVal field As Expression(Of Action(Of T))) As String Dim expression = DirectCast(field.Body, MemberExpression) Return expression.Member.Name End Function Which works like this: NameForProperty(Of String)(Function (s) s.Length) ' ==> returns "Length" And what I thought would have been the equivalent in C#: string NameForProperty<T>(Expression<Action<T>> field) { var expression =

Dynamically building an expression tree

我的梦境 提交于 2019-12-23 20:02:18
问题 I'm following this excellent example: Convert Linq to Sql Expression to Expression Tree In my case I'm trying to build an expression tree where the type to be filtered is only known at run time, and is expressed as a string. In the above example the type Region is already known and typed directly: ParameterExpression pe = Expression.Parameter(typeof(Region), "region"); In my application I've been able to rewrite this as: ParameterExpression pe = Expression.Parameter(Type.GetType("mystring"),

Set field value with Expression tree

孤人 提交于 2019-12-23 17:39:02
问题 I need to create an expression for all fields in a class. So I've adopted this post to my needs: public static void Sample() { var setters = GetFieldSetterExpressions<Account>(); var myAccount = new Account(); setters["AccounHolder"](myAccount, "Giovanni"); setters["Pin"](myAccount, 123); } public static Dictionary<string, Action<T, object>> GetFieldSetterExpressions<T>() where T: class { var dic = new Dictionary<string,Action<T,object>>(); var type = typeof(T); var fields = type.GetFields();

ToString on Expression Trees produces badly formatted output

限于喜欢 提交于 2019-12-23 16:26:49
问题 When I use Expression.ToString() to convert an Expression Tree into human readable form, the result is something like this: x => ((x.ID > 2) OrElse (x.ID != 6)) x => ((x.ID > 2) AndAlso (x.ID != 6)) Ideally, I would want the output to show the operators instead of "OrElse" and "AndAlso": x => ((x.ID > 2) || (x.ID != 6)) x => ((x.ID > 2) && (x.ID != 6)) As a workaround, I could use the string.Replace() method.. .Replace("AndAlso", "&&") .Replace("OrElse", "||") but that has obvious weaknesses