score:2
the lambda in your example has "closed over" the testvalue
variable, meaning the compiler has captured it as a field of the same name in an automatically generated class called consoleapplication1.program+<>c__displayclass1>
. you can use normal reflection to get the current value of that field by casting the right hand-side of the binary expression into a memberexpression.
var testvalue = "hello";
var expr = (expression<func<string, bool>>) (x => x == testvalue);
var rhs = (memberexpression) ((binaryexpression) expr.body).right;
var obj = ((constantexpression) rhs.expression).value;
var field = (fieldinfo) rhs.member;
var value = field.getvalue(obj);
debug.assert(equals(value, "hello"));
testvalue = "changed";
value = field.getvalue(obj);
debug.assert(equals(value, "changed"));
alternatively you can change your variable into a constant.
const string testvalue = "hello";
var expr = (expression<func<string, bool>>) (x => x == testvalue);
var value = ((constantexpression) ((binaryexpression) expr.body).right).value;
debug.assert(equals(value, "hello"));
score:0
instead of doing this by yourself, have a look at partialevaluator
from matt warren. it replaces all references to constants with the constants themselves.
Source: stackoverflow.com
Related Query
- Expression Tree with string assignment and getting value
- Intersect two lists and return the similarity with the preserved order of the original first string value
- Split string and set default value if separator not found WITH LINQ
- Combine property selector expression tree and value to create a predicate for EF filtering - create filter from lambda selector and value
- Problem with Convert.ToInt32 and getting error Index and length must refer to a location within the string
- Linq in conjunction with custom generic extension method build error - An expression tree may not contain an assignment operator?
- Usage recursive hierarchy tree structure in Linq query with multible tables and return some Json Value
- Expression tree for groupby with where clause and than select
- Create LINQ expression Tree with multiple OR and AND condition
- Expression Tree Group and Select using Column with any Type
- How do i obtain foreign table field using string value for fieldname with reflection and entity framework
- linq to sql c#: replace a foreign key with its string value and keep the table structure
- Storing result of linq in keyvaluepair with key and value after spliting the String using comma seperator
- C#: Recursively get Dictonary value with deep string expression
- Delegate method signature with object and value assignment
- Get Parent based on string value and all its children with Entity Framework Core
- Building an Expression tree for Comparison Operations with value concatenated e.g ">= 1"
- Split field a string and compare each value with string linq c#
- How to perform .Max() on a property of all objects in a collection and return the object with maximum value
- What does this C# code with an "arrow" mean and how is it called?
- Convert an array to dictionary with value as index of the item and key as the item itself
- Getting Nth value with Linq
- How Build Lambda Expression Tree with multiple conditions
- lambda expression join multiple tables with select and where clause
- Selecting multiple columns with linq query and lambda expression
- How do I create a Linq expression tree with an F# lambda?
- Find an XElement with a certain attribute name and value with LINQ
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- when and in what scenario to use Expression Tree
- How to build a dynamic AND OR linq expression tree in a loop
More Query from same tag
- The most simple LINQ to XML query. But I can't get it
- How to distinct a list using LINQ?
- Returning Wrong value in LINQ .Any()
- Converted sql query to linq but not getting any result
- linq2db update cannot find definition for .Set method MySQL
- How to do inner join, group by and count using linq
- Map class to tables with different table names but same column structure
- Need help converting a SQL left join query into LINQ format
- User follow query that fetches the first top post for each user being follow
- LINQ search though a list of string arrays for a particular string
- Using Linq.Dynamic where method with a dynamic list gives error
- How to perform an update using Linq or Lambda?(C#, Asp.net,Linq , Lambda)
- Using string methods in EF LINQ queries
- Select in LINQ all keys from List of List of KeyValuePairs
- Why is Null an Invalid LINQ projection?
- Is there a Linq equivalent for creating collection from subset of an object used in a previous collection
- Creating list of users when developing an organisational hierarchy in C#
- How to separate all consecutive Objects using Linq (C#)
- Need help finishing or rewriting this algorithm for navigating a Generic Collection
- Display table names of dataset in combox using LINQ
- LINQ group by and filter particular record
- Get common records from list of list in linq
- linq to sql - query to find items without children
- Group Concatenation with Linq
- Use LINQ to omit some entries in the value part of a dictionary and project this into a new dictonary maintaing original keys
- How to create dictionary from a grouped elements?
- Entity Framework linq query to find records with turkish characters
- LINQ Order by question
- union in two linq statements and remove the duplicate
- How do I make my method generic whereby I can pass in properties?