expression-trees

Create fully dynamic where clause with expression tree and execute on IQueryable

做~自己de王妃 提交于 2019-12-21 12:10:59
问题 At point (3) in my code I have defined a query called query1 in which I defined a .Where lambda expression. This query is in some way dynamic but still contains static elements, it always refers to the Type Employee and its (int) property ClientID. Now I very much like to make the refering to the type and its property dynamic, based on the method parameters which by example are shown below point (1). What I tried to so far is making the static part of the query defined under point (3) fully

LINQ Expression Tree Any() inside Where()

独自空忆成欢 提交于 2019-12-21 11:37:41
问题 I'm trying to generate the following LINQ query: //Query the database for all AdAccountAlerts that haven't had notifications sent out //Then get the entity (AdAccount) the alert pertains to, and find all accounts that //are subscribing to alerts on that entity. var x = dataContext.Alerts.Where(a => a.NotificationsSent == null) .OfType<AdAccountAlert>() .ToList() .GroupJoin(dataContext.AlertSubscriptions, a => new Tuple<int, string>(a.AdAccountId, typeof(AdAccount).Name), s => new Tuple<int,

How to assign a value via Expression?

时光总嘲笑我的痴心妄想 提交于 2019-12-21 09:24:24
问题 This would be very simple if I were able to assign via a Lambda expression (below) //An expression tree cannot contain an assignment operator Expression<Func<ComplexObj, object>> expression = obj => obj.Contacts[0].FirstName = "Tim"; This code above is invalid due to the assignment operator. I need to pass a lambda expression in order to identify the property in the complex object that needs to be set. In some cases the complex object has List and therefor duplicate object types and names

Build IQueryable.Any with Expression Trees for LINQ queries

好久不见. 提交于 2019-12-21 09:18:10
问题 I'm building a SQL "WHERE" clause dynamically using the System.Linq.Expressions.Expression class. It works well for simple clauses, e.g. to add "PhaseCode = X" clause, I do the following: var equalTarget = Expression.Constant(phaseCode, typeof(int?)); var phaseEquals = Expression.Equal(Expression.PropertyOrField(projParam, "PhaseCode"), equalTarget); However, now I'm trying to build an expression that will returns the record if a project has been assigned to a particular group. Project and

Building Expression Tree for string.Contains [duplicate]

本秂侑毒 提交于 2019-12-21 05:06:16
问题 This question already has answers here : How do I create an expression tree to represent 'String.Contains(“term”)' in C#? (4 answers) Closed 5 years ago . I'm struggling to build an expression tree so I can dynamically do filtering on some data. I have come up with this, but it fails at the var lambda = line foreach (var rule in request.Where.Rules) { var parameterExpression = Expression.Parameter(typeof(string), rule.Field); var left = Expression.Call(parameterExpression, typeof(string)

Dynamically create a class by interface

拥有回忆 提交于 2019-12-21 04:51:45
问题 I have some expirience with .Net Expressions , when I'm able to dynamically generate methods. It's fine, it's good. But now I need to generate a whole class, and it seems that the only way to do it is Emit whole IL which is totally inacceptable (it's impossible to support). Assume we have following interface: public interface IFoo { [Description("5")] int Bar(); [Description("true")] bool Baz(); } which should be converted to: public class Foo : IFoo { public int Bar() => 5; public bool Baz()

Compiling lambdas and invoking delegates on the device in Monotouch

本小妞迷上赌 提交于 2019-12-21 04:06:14
问题 I am currently porting a .NET codebase in MonoTouch and I'm currently working on a method that receives an Expression<T> . I'm trying to compile it, and then dynamically invoke it. Here's what I did: // Here's an example of what I could receive Expression<Action<int>> expression = (a => Console.WriteLine (a * 2)); // And here's what I'm trying to do to invoke it expression.Compile().DynamicInvoke(6); This works fine in the iOS Simulator, the result "12" is printed in my console. But then I

Expression.Or, The parameter 'item' is not in scope

青春壹個敷衍的年華 提交于 2019-12-21 01:07:13
问题 I am trying to write a static function to Or two expressions, but recieve the following error: The parameter 'item' is not in scope. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: The parameter 'item' is not in scope. the method: public static Expression<Func<T, bool>> OrExpressions

Expression Trees with dynamic parameter

南楼画角 提交于 2019-12-20 12:41:53
问题 I want to convert this: Func<dynamic, object> myFunc = t => return t.Name + " " + t.Surname; Into an Expression Tree. What I have came up with, is this: ParameterExpression target = ExpressionParameter(typeof(dynamic), "target"); ParameterExpression result = ExpressionParameter(typeof(object), "result"); BlockExpression block = Expression.Block( new [] { result }, Expression.Assign( result, Expression.Add( Expression.Add( Expression.Property(target, "Name"), Expression.Constant(" ", typeof

How to create a Expression.Lambda when a type is not known until runtime?

牧云@^-^@ 提交于 2019-12-20 10:33:41
问题 This is best explained using code. I have a generic class that has a method that returns an integer. Here is a simple version for the purposes of explaining... public class Gen<T> { public int DoSomething(T instance) { // Real code does something more interesting! return 1; } } At runtime I use reflection to discover the type of something and then want to create an instance of my Gen class for that specific type. That is easy enough and done like this... Type fieldType = // This is the type I