expression-trees

Performance of expression trees

房东的猫 提交于 2019-12-18 12:47:45
问题 My current understanding is that 'hard coded' code like this: public int Add(int x, int y) {return x + y;} will always perform better than expression tree code like this: Expression<Func<int, int, int>> add = (x, y) => x + y; var result = add.Compile()(2, 3); var x = Expression.Parameter(typeof(int)); var y = Expression.Parameter(typeof(int)); return (Expression.Lambda(Expression.Add(x, y), x, y). Compile() as Func<int, int, int>)(2, 3); as the compiler has more information and can spend more

Parsing and Translating Java 8 lambda expressions

大憨熊 提交于 2019-12-18 12:14:37
问题 In C# you can enclose a lambda expression in an expression tree object and then possibly parse it. I was wondering if this is also possible in Java? What I'm looking for is doing something like this: BooksRepository.getAll() .where(b -> b.getIban() == "SomeIban") .and(b -> b.getAuthor() == "SomeAuthor"); //etc. And then the BooksRepository should somehow translate that query to the following RESTful API request based on the predicates specified as lambdas: GET http://mylibrary.com/books?Iban

Lambda Expression Tree Parsing

て烟熏妆下的殇ゞ 提交于 2019-12-18 11:27:17
问题 I am trying to use Lambda Expressions in a project to map to a third party query API. So, I'm parsing the Expression tree by hand. If I pass in a lambda expression like: p => p.Title == "title" everything works. However, if my lambda expression looks like: p => p.Title == myaspdropdown.SelectedValue Using the .NET debugger, I don't see the actual value of that funciton. Instead I see something like: p => p.Title = (value(ASP.usercontrols_myaspusercontrol_ascx).myaspdropdown.SelectedValue)

Lambda Expression Tree Parsing

旧城冷巷雨未停 提交于 2019-12-18 11:26:32
问题 I am trying to use Lambda Expressions in a project to map to a third party query API. So, I'm parsing the Expression tree by hand. If I pass in a lambda expression like: p => p.Title == "title" everything works. However, if my lambda expression looks like: p => p.Title == myaspdropdown.SelectedValue Using the .NET debugger, I don't see the actual value of that funciton. Instead I see something like: p => p.Title = (value(ASP.usercontrols_myaspusercontrol_ascx).myaspdropdown.SelectedValue)

How to get the value of a ConstantExpression which uses a local variable?

丶灬走出姿态 提交于 2019-12-18 10:54:07
问题 I created an ExpressionVisitor implementation that overrides VisitConstant. However, when I create an expression that utilizes a local variable I can't seem to get the actual value of the variable. public class Person { public string FirstName { get; set; } } string name = "Michael"; Expression<Func<Person, object>> exp = p => p.FirstName == name; How in the world do I get the value of the variable "name" from the ConstantExpression? The only thing that I can think of is this: string

Get all 'where' calls using ExpressionVisitor

蹲街弑〆低调 提交于 2019-12-18 04:24:22
问题 I have a query, like such: var query = from sessions in dataSet where (names.Contains(sessions.Username)) where (sessions.Login.TimeOfAction == dt) select new { sessions.Username, sessions.Login, sessions.Logout, sessions.Duration }; I want to implement an ExpressionVisitor to extract BOTH the where clauses as Lambda's, but so far have only been able to get the first using a class called 'InnermostWhereFinder' that comes from the MSDN tutorial on a custom query provider for the TerraServer

Assignment in .NET 3.5 expression trees

ε祈祈猫儿з 提交于 2019-12-18 03:24:27
问题 Is it possible to encode an assignment into an expression tree? 回答1: No, I don't believe so. Certainly the C# compiler disallows it when converting a lambda expression: int x; Expression<Func<int,int>> foo = (x=y); // Assign to x and return value This yields the error: CS0832: An expression tree may not contain an assignment operator 回答2: You should able to do it with .NET 4.0 Library. by import Microsoft.Scripting.Core.dll to your .NET 3.5 project. I am using DLR 0.9 - There might be some

Querying Entity with LINQ using Dyanmic Field Name

妖精的绣舞 提交于 2019-12-18 03:00:27
问题 I have created a dynamic search screen in ASP.NET MVC. I retrieved the field names from the entity through reflection so that I could allow the user to choose which fields they wanted to search on instead of displaying all fields in the view. When the search result is Posted back to the controller, I receive a FormCollection containing the FieldName and the Value. I don't know how many fields are being searched on, and the FormCollection only contains fields that were chosen by the user. I

Get actual return type from a Expression<Func<T, object>> instance

こ雲淡風輕ζ 提交于 2019-12-17 23:46:23
问题 I have a method that accepts a Expression<Func<T, object>> instance. I want to get at the actual data type being returned by a specific expression instance, rather than object . I can get it to work for direct property references, so if I pass in the expression x => x.IntegerProperty I can get a Type reference for an integer. This approach requires converting it to a MemberExpression. However, I can't get it to work for arbitrary expressions. For instance, if the expression is x => x

What are some examples of MemberBinding LINQ expressions?

时光毁灭记忆、已成空白 提交于 2019-12-17 23:39:07
问题 There are three possibilities, but I can't find examples: System.Linq.Expressions.MemberAssignment System.Linq.Expressions.MemberListBinding System.Linq.Expressions.MemberMemberBinding I want to write some unit tests to see if I can handle them, but I don't know how to write them except for the first one, which seems to be new Foo { Property = "value" } where Property = "value" is an expression of type MemberAssignment . See also this MSDN article. 回答1: EDIT This replaces the previous answer