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 Articles
- Traverse a Linq Expression to set the value of a property field
- Expression to get LINQ with Contains to EF for SQL IN() where on entities child's property equals value
- get property value in linq expression
- Linq extension. Change property value in source list
- Getting a property value using a LINQ expression
- When is the value of a property set after a LINQ expression is evaluated?
- Cannot implilcitly convert type int? to int when retrieving and int field value from LINQ expression
- Convert a filter expression based on property name and value to Linq Where clause Func<T, bool>?
- How to use the value from a property accessor LINQ expression in a Contains LINQ expression?
- How to use LINQ to select object with minimum or maximum property value
- LINQ Expression to return Property value?
- Filter linq list on property value
- Get the parameter value from a Linq Expression
- linq remove item from object array where property equals value
- Using a LINQ ExpressionVisitor to replace primitive parameters with property references in a lambda expression
- LINQ - Returning value of a property of an object that is not null
- LINQ "Where" condition -> change value of property
- Linq expression to set all values of an array to a given value
- How to get a value out of a Span<T> with Linq expression trees?
- Using LINQ expression to assign to an object's property
- Linq query to exclude from a List when a property value of List of different type are equal?
- Dynamic LINQ query to get Field value from Database
- 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
- C# LINQ Select objects with the same value of one property join values of other
- LINQ - Assign a Value to Anonymous Type's Read Only Property
- Dynamic LINQ Expression for sorting navigation property
- LINQ Source Code Available
- DataTable: Get Max Value Using LINQ With Criteria Field (GroupBy)
- Best way to assign a value to a property of all objects in a collection using LINQ
- Why is this Linq Where Clause not filtering the results
- Linq add list inside a list in C#
- LINQ List overwrites previous value instead of setting new one
- In Silverlight, add custom property to a generated Linq class + manage events
- Adding additional linq where clauses based on variables
- LINQ Query on Huge List()
- LINQ to objects cast result of query
- LINQ Concate Two List To make one csv list
- Linq XML query - How do I return a node that meets a condition within its own nested nodes?
- Lambda function in LINQ select clause
- Returning a nullable DateTime in LINQ2SQL
- What is the difference between a regular foreach and ForEach LINQ operator when it comes to async/await
- search equals width LINQ
- Linq select group by where syntax
- EFCore Getting Differences between two tables
- How to get data from two tables but with where clause on the second (FOREIGN KEY) table? [LINQ]
- Sometimes Connected CRUD application DAL
- Getting sub list
- Issues on Win Host godaddy
- linq string handling 2 chars in 1 lambda expression