expression-trees

Currying Expressions in C#

筅森魡賤 提交于 2019-12-30 09:53:28
问题 I am trying to build up an expression tree that I can feed into Linq2SQL so that it will generate a nice clean query. My purpose is to build a filter that takes an arbitrary set of words to AND and NOT (or OR and NOT) together. Because I want to vary the fields that I search on I preferably want to compose a list of Expresssion<Func<T, string, bool>> 's together (where T is the entity I am operating on) by calling a variety of helper functions. Then I would receive an array of words and loop

Currying Expressions in C#

给你一囗甜甜゛ 提交于 2019-12-30 09:53:08
问题 I am trying to build up an expression tree that I can feed into Linq2SQL so that it will generate a nice clean query. My purpose is to build a filter that takes an arbitrary set of words to AND and NOT (or OR and NOT) together. Because I want to vary the fields that I search on I preferably want to compose a list of Expresssion<Func<T, string, bool>> 's together (where T is the entity I am operating on) by calling a variety of helper functions. Then I would receive an array of words and loop

Creating dynamic expression for entity framework

孤街醉人 提交于 2019-12-30 07:08:08
问题 I've created a generic expression builder that builds up a predicate based on collection of conditions. I pass the predicate to a generic method in the repository. I think that expression builder works fine and creates the desired predicate although the SQL script generated by Entity Framework is not as I expected. I've read many questions and article regarding dynamic query or LinqKit and expression builder to this issue and the most relevant was this comment. I really appreciate if you

Is there a particular reason LinqKit's expander can't pick up Expressions from fields?

那年仲夏 提交于 2019-12-29 11:34:17
问题 I'm using LinqKit library which allows combining expressions on the fly. This is a pure bliss for writing Entity Framewok data acess layer because several expressions can optionally be reused and combined, which allows both for readable and efficient code. Consider following piece of code: private static readonly Expression<Func<Message, int, MessageView>> _selectMessageViewExpr = ( Message msg, int requestingUserId ) => new MessageView { MessageID = msg.ID, RequestingUserID =

Incorrect number of parameters supplied for lambda declaration

China☆狼群 提交于 2019-12-29 07:43:38
问题 Please have a look at my code: I'm trying to create myCar with an Expression Tree. I get an ArgumentException on this line var m = Expression.Lambda<Func<Engine,... The message is Incorrect number of parameters supplied for lambda declaration . public class Engine { public string Name { get; private set; } public Engine(string name) { Name = name; } } public class Car { private readonly Engine engine; public Car(Engine engine) { this.engine = engine; } public string GetEngineName(){return

Building a Custom Expression Tree in Spirit:Qi (Without Utree or Boost::Variant)

≯℡__Kan透↙ 提交于 2019-12-28 16:02:48
问题 First of all, if it is much easier using either Boost Variant or Utree, then I will settle with them, and i will try to solve my issues with them in another topic. However, i would very much like to be able to build a tree like i have below. Background, ignore if you would like to go straight to the issue: I would like to be able to build an expression tree which parses something like "({a} == 0) && ({b} > 5)" or a standard mathmatic expression "(2 * a) + b" I will then define what a and b

Creating delegates dynamically with parameter names

最后都变了- 提交于 2019-12-28 06:15:29
问题 Hi I'm trying to create a function that dynamically creates a delegate with the same return value and the same parameters as a MethodInfo it receives as parameter and also and this is very important the same parameter names! What I did so far is create a function that returns a lambda that receives the same parameter types and has the same return value as the MethodInfo but it doesn't have the parameter names: static void Example() { Person adam = new Person(); MethodInfo method = typeof

Creating delegates dynamically with parameter names

南楼画角 提交于 2019-12-28 06:15:08
问题 Hi I'm trying to create a function that dynamically creates a delegate with the same return value and the same parameters as a MethodInfo it receives as parameter and also and this is very important the same parameter names! What I did so far is create a function that returns a lambda that receives the same parameter types and has the same return value as the MethodInfo but it doesn't have the parameter names: static void Example() { Person adam = new Person(); MethodInfo method = typeof

variable '' of type '' referenced from scope '', but it is not defined

╄→尐↘猪︶ㄣ 提交于 2019-12-28 04:22:05
问题 Well, the following code is self-explaining; I want to combine two expressions into one using And operator. The last line causes rune-time the error: Additional information: variable 'y' of type 'System.String' referenced from scope '', but it is not defined Code: Expression<Func<string, bool>> e1 = y => y.Length < 100; Expression<Func<string, bool>> e2 = y => y.Length < 200; var e3 = Expression.And(e1.Body, e2.Body); var e4 = Expression.Lambda<Func<string, bool>>(e3, e1.Parameters.ToArray())

How do I set a field value in an C# Expression tree?

帅比萌擦擦* 提交于 2019-12-28 03:24:05
问题 Given: FieldInfo field = <some valid string field on type T>; ParameterExpression targetExp = Expression.Parameter(typeof(T), "target"); ParameterExpression valueExp = Expression.Parameter(typeof(string), "value"); How do I compile a lambda expression to set the field on the "target" parameter to "value"? 回答1: .Net 4.0 : now that there's Expression.Assign , this is easy to do: FieldInfo field = typeof(T).GetField("fieldName"); ParameterExpression targetExp = Expression.Parameter(typeof(T),