expression-trees

Retrieving an Expression from a property and adding it to an expression tree

别等时光非礼了梦想. 提交于 2019-12-05 13:24:55
I've tried to simplify this example, as the actual code I'm playing with is more complex. So while this example may seem silly, bear with me. Let's say I'm working with the AdventureWorks database and I decide I want to add a property called Blarg to the Product table that returns an expression that contains code I would like to use in several places: public partial class Product { public Expression<Func<Product, string>> Blarg { get { return product => product.ProductModelID.HasValue ? "Blarg?" : "Blarg!"; } } } What I want to do is create an expression expression tree, have it get the

Building a custom predicate to act as a filter using a foreach loop

微笑、不失礼 提交于 2019-12-05 13:10:18
I need to filter a list of documents by passing them to a custom filter that I'm struggling to build dynamically using a foreach loop : var mainPredicate = PredicateBuilder.True<Document>(); // mainPredicate is combined to other filters successfully here ... var innerPredicate = PredicateBuilder.False<Document>(); foreach (var period in periods) { var p = period; Expression<Func<Document, bool>> inPeriod = d => d.Date >= p.DateFrom && d.Date <= p.DateTo; innerPredicate = innerPredicate.Or(d => inPeriod.Invoke(d)); } mainPredicate = mainPredicate.And(innerPredicate); This last line : documents

Expression tree - how to get at declaring instance?

吃可爱长大的小学妹 提交于 2019-12-05 12:20:17
I'm a newbie when it comes to expression trees, so I'm not sure how to ask this question or what terminology to use. Here's an overly-simplifed version of what I'm trying to do: Bar bar = new Bar(); Zap(() => bar.Foo); public static void Zap<T>(Expression<Func<T>> source) { // HELP HERE: // I want to get the bar instance and call bar.Zim() or some other method. } How can I get to bar inside the Zap method? Since the expression passed into your Zap method is a tree, you just need to walk the tree using an Expression Tree Visitor and look for the first ConstantExpression in the expression. It

How to create a dynamic 'contains or LIKE' Expression to be used with Linq against OData service

不羁岁月 提交于 2019-12-05 12:16:56
I'm try'n to create a dynamic query tool using System.Linq.Expressions.Expression (WPF/c#4.0) It runs against an OData Service. So far, all is working as long as I limit the conditions to build in options like Equal(..), GreaterThan(..) etc. There seems to be no build in contains/Like condition so I tried building my own. There are a handful of articles already out there. One I tried is How to create a System.Linq.Expressions.Expression for Like? . Now if I use the above solution, the resulting where expression is whereCallExpression = {Convert([10000]).Expand("A,B").Where(clt => MyLike(clt

How to create an Expression Tree to do something similar to the SQL “Like ” command

萝らか妹 提交于 2019-12-05 11:19:29
I’m working on some expression tree code written by a colleague and am looking into the possibility of adding additional expressions. It currently supports: equals, not-equals, IsNull etc. I need to add something that will allow it to use a wildcard comparison similar to the SQL “Like” command or using regular expressions. At the moment the code parses an XML file and extracts the data which is then processed using code similar to the line shown below. This is an example of the “Equal” expression. “callExp” is a MemberExpression that basically holds the field name of my table (Entities) and

Create delegate from constructor

守給你的承諾、 提交于 2019-12-05 09:29:22
Using reflection, I'm trying to create a delegate from a parameterless constructor like this: Delegate del = GetMethodInfo( () => System.Activator.CreateInstance( type ) ).CreateDelegate( delType ); static MethodInfo GetMethodInfo( Expression<Func<object>> func ) { return ((MethodCallExpression)func.Body).Method; } But I get this exception: "Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type." What will work? Note that CreateDelegate was moved, for this profile at least, since the previous version of .NET. Now it's

Java Expression Trees [closed]

百般思念 提交于 2019-12-05 09:12:30
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Is there an equivalent of .net 's Expression Trees that underly LINQ for the JVM ? I would like to implement some LINQ like code structures in Scala and I am wondering if I have to roll my own expression tree library also. Update: I am not interested in a linq equivalent itself. .net has a large set of

Convert Expression<Func<T, T2, bool>> to Expression<Func<T2, bool>> by introducing a constant for T

时光怂恿深爱的人放手 提交于 2019-12-05 06:53:45
I have an expression in the format of Expression<Func<T, T2, bool>> that I need to convert into an expression on the format of Expression<Func<T2, bool>> by replacing the T in the first expression with a constant value. I need this to stay as an expression so I can't just Invoke the expression with a constant as the first parameter. I've looked at the other questions here about expression trees but I can't really find a solution to my problem. I suspect I have to walk the expression tree to introduce the constant and remove one parameter but I don't even know where to start at the moment. :(

Python: Optimizing, or at least getting fresh ideas for a tree generator

可紊 提交于 2019-12-05 06:41:30
问题 I have written a program that generates random expressions and then uses genetic techniques to select for fitness. The following part of the program generates the random expression and stores it in a tree structure. As this can get called billions of times during a run, I thought it should be optimized for time. I'm new to programming and I work (play) by myself so, as much as I search on the inernet for ideas, I'd like some input as I feel like I'm doing this in isolation. The bottlenecks

How to reflect over T to build an expression tree for a query?

五迷三道 提交于 2019-12-05 06:35:33
问题 I'm trying to build a generic class to work with entities from EF. This class talks to repositories, but it's this class that creates the expressions sent to the repositories. Anyway, I'm just trying to implement one virtual method that will act as a base for common querying. Specifically, it will accept a an int and it only needs to perform a query over the primary key of the entity in question. I've been screwing around with it and I've built a reflection which may or may not work. I say