score:3
I don't believe this is currently possible to do with the compiler's help (as of C# 4.0), but you could of course do it "by hand" with Expression.Invoke.
Creates an InvocationExpression that applies a delegate or lambda expression to a list of argument expressions.
In your case, this would look something like:
Expression<Func<int, int>> f = x => x + x;
var parameter = Expression.Parameter(typeof(int),"y");
var body = Expression.Add(Expression.Constant(2), Expression.Invoke(f, parameter));
// Loosely speaking: y => 2 + f(y)
var g = Expression.Lambda<Func<int, int>>(body, parameter);
LINQKit provides the Invoke
/Expand
extension-methods that do the leg-work for you, giving you back the ability to use lambdas for this. This will let you do:
Expression<Func<int, int>> f = x => x + x;
Expression<Func<int, int>> g = x => 2 + f.Invoke(x);
// Note: 'Inlines' the invocation into the target expression.
var finalExpression = g.Expand();
..which is very close to the syntax you had originally wanted.
One point to note about InvocationExpression
s is that some LINQ providers (such as LINQ to Entities) don't like them at all; so you'll have often have to 'expand' out the invoked expression so that it looks like one single expression to the provider. You can of course do this by hand, but LINQKit again comes in handy with the AsExpandable
extension.
Source: stackoverflow.com
Related Articles
- How can I, using the C# lambda expression syntax, invoke another expression?
- Query XML source in LinqPad using lambda syntax
- How to write the same code using Lambda Expression
- Get a iqueryable items which do not exists on another list using lambda expression
- Can you create a simple 'EqualityComparer<T>' using a lambda expression
- LINQ: Get all selected values of a CheckBoxList using a Lambda expression
- Simple Examples of joining 2 and 3 table using lambda expression
- How can I combine two lambda expressions without using Invoke method?
- How to query a nested list using a lambda expression
- How to Convert LINQ Comprehension Query Syntax to Method Syntax using Lambda
- Convert or map a list of class to another list of class by using Lambda or LINQ?
- Get N max numbers from a List<int> using lambda expression
- Using a LINQ ExpressionVisitor to replace primitive parameters with property references in a lambda expression
- Equivalent of SQL Between Statement Using Linq or a Lambda expression
- Check if a String value contains any number by using Lambda Expression
- String Join Using a Lambda Expression
- Compound Select using lambda expression
- Lambda expression syntax
- Linq - Using array in Lambda expression to fetch multiple records
- How to reuse a linq expression for 'Where' when using multiple source tables
- Get Value and Count of that value using LINQ or lambda expression
- Use lambda expression in another lambda expression
- Syntax to refer a method returning an Expression to another method?
- Execute expression on another IQueryable source
- How to call a lambda using LINQ expression trees in C# / .NET
- How to find the difference of two LIST<object> in c# using lambda expression
- Access property in lambda expression from string when using LINQ
- Create Lambda Expression Selector For New Class Using Expression Tree
- Find Count using lambda Expression
- Include using Lambda expression
- C# : How to add data data from a ListView control to a Dictionary
- SequenceEqual linq group
- Expression cannot contain lambda expressions
- How do I check if a SQL Server 2005 TEXT column is not null or empty using LINQ To Entities?
- LINQ to validate outline numbering (1.1, 1.2, etc)
- Getting around lack of 'Contains' in Linq To Entities
- Error When Adding Creating List From View
- re-using a linq variable
- Increase in time taken to run LINQ query .asparallel when creating new separate tasks in C#
- building dynamic linQ queries
- Substring in linq
- Sql YEAR(),MONTH() in Linq
- Could i mix values from local data with values returned from database while using LINQ to SQL?
- Create an asynchronous LinQ query
- Linq GroupBy throws ArgumentOutOfRangeException
- Complex Linq c# query
- Output ICollection dropdownlist method corresponding to collection form query
- Remove entry of a Bindinglist from a Linq results. - 2 birds in one shot?
- Can i have only one method to delete record from passed table name and id using Entity Framework and Linq?
- how to get data from linq database model