score:3
Accepted answer
Just set the body to be the second parameter, p2
. You already have the reference.
var p1 = Expression.Parameter(relationshipItems.ElementType, "i");
var p2 = Expression.Parameter(instanceEntities.ElementType, "o");
var body = p2;
var lambda = Expression.Lambda(body, p1, p2);
score:0
If, for the sake of example, we assume p1 is an int and p2 is a string then this:
var p1=Expression.Parameter(typeof(int),"i");
var p2=Expression.Parameter(typeof(string),"o");
var lambda=Expression.Lambda(p2,p1,p2);
var function=lambda.Compile() as Func<int,string,string>;
var result=function(10,"hello");
will generate a function that returns "o". The lambda will return the value in the last expression in its body, which in the above example is just the parameter p2.
Source: stackoverflow.com
Related Articles
- What expression method to use to get back parameter?
- What is the typeArguments parameter of the Expression.Call Method when making a Expression Tree?
- C# Pass Lambda Expression as Method Parameter
- LINQ: Passing lambda expression as parameter to be executed and returned by method
- Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'
- How do I build Expression Call for Any Method with generic parameter
- How to implement method with expression parameter c#
- Linq expression to return the first overloaded method that takes 1 string parameter
- pass a bool expression as a method parameter
- Dynamicaly changing the type of a parameter in an Expression without knowing the target type at code time
- How to use a method with an out parameter in a lambda expression
- C# exclude duplicate code from two method returning expression
- Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'
- What is the best way to find length of split characters from the given string by using String.Split() Method or Linq Lambda Expression in C#
- pass lambda expression as callback to an extension method and pass parameter to it
- Pass lambda expression for Any() in Where() query as method parameter
- C# LINQ Expression as parameter for DataTable method
- Create a generic method where a expression can be passed as parameter to make dynamic queries
- Executing linq lamda expression as parameter in System.Reflection Invoke Method calling
- Finding and parsing the where clause in a method that has a LINQ expression for a parameter
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- What is the use of Enumerable.Zip extension method in Linq?
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- What is the opposite method of Any<T>
- What are Expression Trees, how do you use them, and why would you use them?
- What does this C# code with an "arrow" mean and how is it called?
- How to resolve Value cannot be null. Parameter name: source in linq?
- Expression references a method that does not belong to the mocked object
- What is the Efficiency and Performance of LINQ and Lambda Expression in .Net?
- How to remove duplicates from a List<List<T>> for value types T?
- Linq to SQL, is there support for a mid-string % wildcard?
- C# How do I use a variable in a Linq select statement using Query Syntax
- LINQ to CRM One to Many results without flattening
- server side operation only return data to grid for particular page
- SQL LINQ Convert to String
- Accesing object properties held in List<T> after LINQ group by procedure
- ASP.NET MVC4 return output from a stored procedure with Entity Framework
- Average numbers associated with same date and extract date from datetime using linq
- Linq Multiple Joins - Too many results
- How to change the data layout of a data table with LINQ
- AsParallel() executing sequentially
- Passing LINQ var to base add method C#
- Numbers of iteration generated in a single LINQ query
- C# Retrieving the absolute value closest to 0 from a sum in a list
- Recursive LINQ calls
- Please help me convert a Linq Query to a SQL Query... Desperate
- LINQ Except() Method Does Not Work
- Expression<Func<T,bool>> adds an unwanted Convert when created in generic method
- Filter the "Includes" table on Entity Framework query