expression-trees

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

浪子不回头ぞ 提交于 2019-12-22 08:05:57
问题 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

Build expression tree for LINQ using List<T>.Contains method

*爱你&永不变心* 提交于 2019-12-22 06:49:33
问题 Problem I'm working on refactoring some LINQ queries for several reports in our web application, and I'm attempting to move some duplicate query predicates into their own IQueryable exension methods so we can reuse them for these reports, and reports in the future. As you can probably infer, I've already refactored the predicate for groups, but the predicate for codes is giving me problems. This is an example of one of the report methods I have so far: DAL method: public List<Entities

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

本小妞迷上赌 提交于 2019-12-22 06:47:37
问题 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

Expression.Lambda and query generation at runtime, nested property “Where” example

筅森魡賤 提交于 2019-12-22 04:27:11
问题 I found very nice answer on a question about building Expression Tree for Where query. Expression.Lambda and query generation at runtime, simplest "Where" example Can someone help me and show me how this example could be implemented in the scenario with nested property. I mean instead of: var result = query.Where(item => item.Name == "Soap") With that solution: var item = Expression.Parameter(typeof(Item), "item"); var prop = Expression.Property(item, "Name"); var soap = Expression.Constant(

Expression tree for a member access of depth > 1

一个人想着一个人 提交于 2019-12-22 01:36:52
问题 public class Job { public string Name { get; set; } public int Salary { get; set; } } public class Employee { public string Name { get; set; } public Job Job { get; set; } } If I want to create an expression tree of a member access to Employee.Name this is what I do: var param = Expression.Parameter(type, "x"); var memberAccess = Expression.PropertyOrField(param, memberName); return Expression.Lambda<Func<TModel, TMember>>(memberAccess, param); What is the equivalent to this for a member

Can I generate an async method dynamically using System.Linq.Expressions?

南笙酒味 提交于 2019-12-22 01:28:11
问题 I know the compiler can't convert an async lambda expression to an expression tree, but is it possible to generate the expression tree manually ? var expr = Expression.Lambda<Func<Task>>( // how do I use 'await' in the body here? ); var func = expr.Compile(); I can't find any method related to async or await in the Expression class, but perhaps there's another way? 回答1: await involves significant compiler re-writing; the generated IL is quite dissimilar to the original C#, with variable

Linq: Group by multiple columns using Expression-tree syntax

▼魔方 西西 提交于 2019-12-22 01:18:51
问题 I would like to change the following code so as to handle grouping of more than 1 property private Expression<Func<ProfileResultView, string>> DynamicGroupBy(string propertyName) { var parameterExp = Expression.Parameter(typeof(ProfileResultView), "x"); var memberExp = Expression.PropertyOrField(parameterExp, propertyName); return Expression.Lambda<Func<ProfileResultView, string>>(memberExp, parameterExp); } so then this would be translated to GroupBy(x => new { x.Column1, x.Column2 }) how

How do I dynamically construct a predicate method from an expression tree?

廉价感情. 提交于 2019-12-22 00:19:06
问题 Here's the scenario: Silverlight 4.0, DataGrid, PagedCollectionView itemssource. The objective is to apply a Filter to the PCV. The filter needs to be a Predicate<object>(Method) - where Method implements some logic against the object and returns true/false for inclusion. What I have is a need to optionally include 3 different criteria in the filter logic and explicit code quickly gets ugly. We don't want that, do we? So I see that there is a way to build an expression tree using

Implementing a prefix notation expression parser using Irony

喜欢而已 提交于 2019-12-21 21:40:04
问题 I'm trying to parse user-inputted prefix notation logical expressions using a context-free-grammar with the Irony library. This is for a class assignment so if anybody happens to be an expert on this, I would love to know more. I need to accept a user-inputted logical expression of the following format: and P Q -- (meaning P ^ Q) or P Q -- (meaning P v Q) not P -- (meaning ~P) imp P Q -- (meaning P -> Q) I'm attempting to parse these into expression trees using a context free grammar I'm

Expression tree depth limitations

假如想象 提交于 2019-12-21 17:39:03
问题 I'm facing a problem trying to call Compile() on the LambdaExpression of type Expression<Func<MyType, bool>> which has a depth around 400. And lesser values do not cause any problems. And I can't find anything about such kind of limitation. Can anyone clarify this? Can I increase this limit? upd: Sorry, forgot to mention, I'm getting StackOverflowException: An unhandled exception of type 'System.StackOverflowException' occurred in System.Core.dll {Cannot evaluate expression because the