Questions tagged [expression-trees]
Expression Trees are an abstract representation of code in a tree structure where each node of the tree represents a programming construct (conditional, assignment, method call, etc.)
expression-trees
2,109
questions
1
vote
2
answers
105
views
Compiled lambda expression leading to new delegate allocations, whereas non-expression version does not
This compiled expression tree...
var param = Expression.Parameter(typeof(int));
var innerParam = Expression.Parameter(typeof(Action<int>));
var inner = Expression.Lambda(innerParam.Type, ...
3
votes
2
answers
98
views
Expression trees - invoke lambda during loop leads to variable leaking
I have found little issue with expression trees and would like to know whether such is a bug or a feature. I have this code. The first function assigns one to a variable and returns it.
static class ...
0
votes
0
answers
70
views
Creating a Lambda for LINQ from code gives error message "Parameter could not be translated"
I am attempting to build a lambda from data.
The module entity has an AlgNo property of type string.
I am trying to get a simple sample code to run before attempting the more advanced case.
Consider ...
0
votes
0
answers
64
views
Expression tree - how to check if element of a list fulfills specific conditions?
I'm using an
Expression<Func<T, object?>> DatabaseProjection which defines what values I want to extract from T. In specific case, I will get a list of objects (string or int):
...
0
votes
1
answer
66
views
Expression.Call on array with a custom function
I have a list of items, I want ,via expression, to filter out the items using my own custom function.
Something like this
return Items.Where(Foo)
private static bool Foo(Item item) { item.Name.Equals(&...
0
votes
0
answers
43
views
Create expression trees using generic arguments which involves nested properties [duplicate]
I am trying to create a dynamic query generator, using generic arguments.
For example:
Expression<Func<T, bool>> CreateExpression<T>(
ParameterExpression parameter,
List<...
1
vote
1
answer
43
views
Accessing x levels of n-ary tree to manipulate a node and add child node with recursion
I have a problem I'm trying to solve where I can take in a variety of expression of different lengths and complexities and insert them as nodes into an n-ary (or non-binary) tree.
I need to pinpoint ...
4
votes
3
answers
186
views
Is there no way for Csharp to write a truly static λ expression?
Language : C#
.Net Version : 8.0
IDE : VS2020 / VSCode
OS : Windows11
Is there no way for Csharp to write a truly static λ expression?
I studied currying today and wrote a ...
2
votes
2
answers
392
views
How to convert Func<T, bool> to Expression<Func<T, bool>>
I use this code but it gets an error that:
Error: System.ArgumentException: Expression of type '...GreenPaperItem' cannot be used for parameter of type 'System.Runtime.CompilerServices.Closure' of ...
0
votes
1
answer
118
views
Unable to resolve expression constants from variable
Following this post marked answer, I've copied Evaluator.cs into my project for partial evaluations to be able to resolve local variables of the expression. Regardless, it seems I'm missing something ...
1
vote
0
answers
61
views
How does .Any() access entity property when built with an expression tree?
First of all, this code works but I don't understand why it works. This question is asking why I can access a property in an expression tree when I don't think I should.
Let's start by establishing ...
0
votes
0
answers
82
views
Expression tree to sql, Guid constants are translated to string in database and can't be read as Guid
I have a method that returns some dynamic grouping keys:
private Expression<Func<T, object>> GetKeySelector<T>(Request req,
Expression<Func<T, Guid>> domainKey, ...
/...
0
votes
0
answers
53
views
CS0834 - A lambda expression with a statement body cannot be converted to an expression tree [duplicate]
I am trying to create an Expression to filter the profiles by creation date
here is the code
private static Expression<Func<Profile, bool>> MonthsPeriodFilter = profile =>
{
var ...
1
vote
1
answer
96
views
C# merge/combine expressions
With the following example classes (which could map to a database table):
class Package
{
public string Name { get; set; }
public decimal Price { get; set; }
public bool Enabled { get; set;...
0
votes
2
answers
71
views
How to NULL check and do a ToLower call along with Contains call dynamically using Expression Tree in C#
I'm trying to dynamically generate the following expression
x => x?.ToLower().Contains(value?.ToLower())
on my Item class.
Here is my Item class:
public class Item
{
public string? Type { get; ...