score:2
well, you'd not only have to traverse the expression tree: you'd also have to convert final property "getter" you encounter into a property "setter". essentially you'd want to find the expression which acts as the "target" of the getter (i.e. the object it's going to get the property of), evaluate it to get the target, then find the corresponding setter for the final property, and call it with the target and the new value.
note that by only requiring the expression tree to represent the "getter", you're losing some of the compile-time safety you might expect... because a caller could pass in a read-only property:
setvalue(c => c.account.name.length, 0); // string.length is read-only
another alternative would be to change your code to make the lambda expression represent the setter instead:
setvalue((c, value) => c.account.name = value, "test");
then you wouldn't even need an expression tree - you could use a simple delegate, and just execute it appropriately.
unfortunately you haven't really given us enough information about what you're trying to achieve to know whether this is a feasible suggestion.
score:1
yes, you will have to traverse the whole expression tree which if you want to work in the general case might be challenging. can't you just do this instead:
setvalue<user>(c => c.account.name = "test");
where setvalue
is defined like this:
public void setvalue<t>(action<t> action)
{
...
}
or without the generic parameter if this will work only with user
.
Source: stackoverflow.com
Related Query
- Traverse a Linq Expression to set the value of a property field
- When is the value of a property set after a LINQ expression is evaluated?
- How to use the value from a property accessor LINQ expression in a Contains LINQ expression?
- LINQ expression to check against a collection and set a default value when the check fails
- Get the parameter value from a Linq Expression
- Linq expression to set all values of an array to a given value
- C# LINQ Select objects with the same value of one property join values of other
- Expression to get LINQ with Contains to EF for SQL IN() where on entities child's property equals value
- what is the Linq expression tree for setting a property of an object?
- Generic method to set the value of a property using expressions/lambda
- Linq expression to find the max value of a List<List<int>>?
- Is possible with LINQ to extract from an array of objects the value of one property and create a new array?
- LINQ filter list based on property value and filter the properties as well
- Can you set a property in a linq query before selecting the item?
- How write a LINQ expression that will return the maximum value of a field?
- get property value in linq expression
- Convert Linq Expression into the string with given property values
- LINQ Query - Selecting a key based upon the value having a property that matches a string?
- Set the list value using linq
- Get the objects with the maximum value for a specific property from a list using LINQ
- In a Linq Expression body how to use the value of a variable instead of a reference to it?
- How to set a DTO's property value directly in LINQ select statement
- Getting the actual field data from a Linq Expression
- C# Linq to select multiple columns by group and apply sum(aggregate function) on value field at the same time
- LINQ to XML set value if it is not null otherwise use default value from the constructor
- The LINQ expression could not be translated about sum of Property TimeSpan
- How to determine if all objects inside List<T> has the same property value using Linq
- Is there a Linq operation to retrieve specific items from a list of items where the item has a property value for a property which should be unique?
- LINQ - get results where a certain property is in this other result set of the property's type
- Linq extension. Change property value in source list
More Query from same tag
- How to get Results from position x till x
- How to find the keys in dictionary object which matches partially?
- Count rows in a text file that meet a condition
- Building a string while looping though the Datatable,inserting, then resetting the string and repeat
- What is more readable?
- How do you compile LINQ queries against CRM 2011?
- prevent unnecessary cross joins in count query of generated sql code
- Create Multiple Objects Single LINQ EF Method
- Multi table join using Entity framework 4.1, should I use lambda or LINQ?
- Fastest way to select all strings from list starting from
- Using Where expression on UIElementCollection
- Using Lambda expressions with Dictionaries
- Can I use LINQ to get more than 100 records back from a Quickbooks Online ServiceContext?
- How do I build up a LINQ => SQL / entities query (with joins) step-by-step?
- How to get same results from select as foreach when using GroupBy()
- "Cannot implicitly convert type 'bool[]' to 'object[]'"
- Convert VBNet LiNQ to C#
- Linq subset from two lists of longs
- linq reflection WInForms
- Group or sort list/array by number of matching keywords
- Can I change the "equal comparator" on LINQ?
- select as field in Linq
- Projection: filling 2 arrays at once
- how to group based on dates then hours?
- Dynamic Data filtering
- C# Linq to query a List to see if a value is empty or null
- Most efficient query to return multiple related entities in Entity Framework Core? (1:M and M:M)
- I need to select particular column based on check box list
- LINQ : Checking against each other item in a list
- Lambdas in Linq AST - why different behaviour?