score:4
no there is no way to do this in c#. the compiler does not support capturing variables by value / const. nor can you convert a non-const value into a const value at runtime in this manner.
additionally the c# compiler only does constant folding during the initial compilation for known constant values. if it was possible to freeze a value at runtime into a constant it wouldn't participate in compiler constant folding because it happens at runtime.
score:0
x
can't be a constant, because you're doing runtime magic to determine what it is. however, if you know that x
and y
don't change, try:
int x = magic(), y = moremagic();
int xovery = x/y;
return i => i + xovery;
i should also mention that even though the compiled il code for i => i + (x/y)
will show the division, the jit compiler is almost certainly going to optimize this away.
score:0
one technique i used in vb2005 was to use a generic delegate factory to effect by-value closures. i only implemented it for subs rather than functions, but it could be done for functions as well. if extended in that way:
functionof.newinv()
would be a static function which would accept as parameters a function (described later), a t3, and a t4. the passed-in function should accept parameters of types t2, t3, and t4, and return a t1. the function returned by newinv would accept one parameter of type t2 and call the passed-in function with that parameter and the ones given to newinv.
the invocation would look something like:
return functionof.newinv((i,x,y) => i+x/y, x, y)
score:0
if you(like me) are creating some expression builder for sql queries you may consirer the following: first create a class variable, make it a constant and then access it like this:
var constant= expression.constant(values);
var start = expression.makememberaccess(constant, values.getmemberinfo(f => f.start));
var end = expression.makememberaccess(constant, values.getmemberinfo(f => f.end));
var more = expression.greaterthanorequal(memberbody, start);
var less = expression.lessthanorequal(memberbody, end);
score:2
the compiler doesn't do this type of "value caching". constant folding is done at compile time for constants only, not for readonly fields and certainly not for local variables which do not have a known value at compile time.
you have to make this yourself, but it has to stay a closure reference (since the value is in fact not determinable at compile time, which is why it is likely to be put in the closure when the expression is built):
int x = magic(), y = moremagic();
int xy = x/y;
return i => i + xy;
Source: stackoverflow.com
Related Query
- Can I capture a local variable into a LINQ Expression as a constant rather than a closure reference?
- How to capture type of a local variable inside a lambda expression in linq
- How can I combine this code into one or two LINQ queries?
- Can you use LINQ with in memory objects rather than SQL Server queries to improve performance?
- Can I filter children rather than nested for loop using LINQ
- How can I code numerous MIN functions into one LINQ to DataSet query
- How can I make a LINQ expression return into a class?
- Can I can convert this C# code into some Linq code?
- How can I convert Linq to Entities Query into expression tree?
- optimise code into linq expression
- How can i convert this code snippet into LINQ format?
- How can I convert IQueryable expression object into LINQ expression?
- how to translate a linq expression into sql string use c# code
- Can I assign the result of a Linq query to the source variable of the same query?
- Unable to create a constant value of type '<Some type>' when using a local variable in linq query, but works with a hard-coded value
- Linq Expression returning null rather than empty set
- How can two left joins from a single table on similar fields be translated into LINQ when more than two tables are involved?
- How can I pull a repeated where clause expression from linq into a function?
- C# Creating a local string variable in linq - lambda expression
- Can this code be converted into a single linq statement?
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- Can I declare / use some variable in LINQ? Or can I write following LINQ clearer?
- Local variable and expression trees
- Building a LINQ expression tree: how to get variable in scope
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
- convert this LINQ expression into Lambda
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
- can linq expression be case insensitive
- Why don't the Linq extension methods sit on IEnumerator rather than IEnumerable?
More Query from same tag
- How to use MethodCallExpression.Update
- Select one node from others with same name with Linq to XML and C#
- Csv DataGridView Conversion to XML Winforms
- Use SQL 2012 FORMAT function with LINQ to Entity Framework
- Why doesn't a null cause a NullReferenceException when mapping to and from a null type?
- Advice on Linq to SQL mapping object design
- How to simplify repetitive OR condition in Where(e => e.prop1.contains() || e.prop2.contains() || ...)
- Use of where in multiple joins to remove rows - linq
- convert code to use Linq
- LINQ conflict - Row not found or changed
- How can i create pagination properly with List<IGrouping<string,Product>>?
- Add second group by key field to existing Lambda Expression
- LINQ update and select
- From Linq To List Using Extension Method
- Entity Framework average with grouping
- I want to retrieve data from multiple tables to service payer into my project
- Exception using CopyToDataTable with "new {..}" LINQ query
- Angular Application Data Binding is Not Working
- Group multiple child tables by a parent table in a single LINQ query
- How to change the content of elements and attributes
- Lambda OrderBy method
- How to find out a child's parent?
- How does the Where method run?
- Nhibernate Linq - select most rated items
- Visual basic LINQ error
- how to retrive crm attribute value with linq when bool is set to false or true
- How do I convert a Group KEY from LINQ into an int?
- Sorting an array by name alphabetically
- How to use EXIST and NOT EXIST clause using lambda expression
- How to get reverse of a series of elements using Enumarable in C#?