Round #230
07 May 2026, 15:36· csharp· meta/llama-3.3-70b-instruct· r12-real-contracts
- Complexity
- band 0
- ZP / UO / OBS
- 3 / 2 / 4
- Journey weight
- 738
- Lightshift ΔL
- —
Prompt
Implement a LINQ expression tree builder for dynamic filtering
Zero-point index (what the engine surfaced)
ZPI-ZPI-0023 | 2026-05-07T15:36:19Z
Implement a LINQ expression tree builder for dynamic filtering
ZERO POINT — 3 entries (structural invariants)
● hsl(7.18, 100%, 18%) class [feature]
● hsl(0.00, 100%, 18%) public [feature]
● hsl(84.10, 100%, 18%) namespace [feature]
UNOBSERVED — 2 entries (implied truths)
○ hsl(340.51, 100%, 18%) IEnumerable of T [feature]
○ hsl(15.38, 100%, 18%) var [feature]
OBSERVED — 4 entries (explicit in request)
◉ hsl(135.38, 100%, 18%) dynamic [feature]
◉ hsl(249.23, 100%, 18%) With expression [feature]
◉ hsl(264.62, 100%, 18%) is expression [feature]
◉ hsl(265.64, 100%, 18%) as expression [feature]
COMPLEXITY │ 9 entries │ spread: 325° │ band: LOW
Baseline response (no ZPI)
Implementing a LINQ Expression Tree Builder for Dynamic Filtering
=============================================================
In this section, we will create a LINQ expression tree builder that allows for dynamic filtering of data. This can be useful in scenarios where the filtering criteria are determined at runtime.
### Expression Tree Builder Class
The following C# code defines a class that builds LINQ expression trees for dynamic filtering:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
public class DynamicFilterBuilder
{
private readonly ParameterExpression _parameter;
public DynamicFilterBuilder(Type entityType)
{
_parameter = Expression.Parameter(entityType, "entity");
}
public Expression<Func<object, bool>> BuildFilterExpression(List<FilterCondition> conditions)
{
Expression body = null;
foreach (var condition in conditions)
{
var propertyExpression = GetPropertyExpression(_parameter, condition.PropertyName);
var valueExpression = GetValueExpression(condition.Value);
var comparisonExpression = GetComparisonExpression(propertyExpression, condition.Operator, valueExpression);
if (body == null)
{
body = comparisonExpression;
}
else
{
body = Expression.AndAlso(body, comparisonExpression);
}
}
return Expression.Lambda<Func<object, bool>>(body, _parameter);
}
private Expression GetPropertyExpression(Expression parameter, string propertyName)
{
return Expression.Property(parameter, propertyName);
}
private Expression GetValueExpression(object value)
{
return Expression.Constant(value);
}
private Expression GetComparisonExpression(Expression propertyExpression, FilterOperator @operator, Expression valueExpression)
{
switch (@operator)
{
case FilterOperator.Equals:
return Expression.Equal(propertyExpression, valueExpression);
case FilterOperator.NotEquals:
return Expression.NotEqual(propertyExpression, valueExpression);
case FilterOperator.GreaterThan:
return Expression.GreaterThan(propertyExpression, valueExpression);
case FilterOperator.GreaterThanOrEqual:
return Expression.GreaterThanOrEqual(propertyExpression, valueExpression);
case FilterOperator.LessThan:
return Expression.LessThan(propertyExpression, valueExpression);
case FilterOperator.LessThanOrEqual:
return Expression.LessThanOrEqual(propertyExpression, valueExpression);
default:
throw new ArgumentException("Invalid operator", nameof(@operator));
}
}
}
public enum FilterOperator
{
Equals,
NotEquals,
GreaterThan,
GreaterThanOrEqual,
LessThan,
LessThanOrEqual
}
public class FilterCondition
{
public string PropertyName { get; set; }
public object Value { get; set; }
public FilterOperator Operator { get; set; }
}
```
### Example Usage
Here's an example of how to use the `DynamicFilterBuilder` class to build a LINQ expression tree for dynamic filtering:
```csharp
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
var people = new List<Person>
{
new Person { Id = 1, Name = "John", Age = 25 },
new Person { Id = 2, Name = "Jane", Age = 30 },
new Person { Id = 3, Name = "Bob", Age = 35 },
};
var filterConditions = new List<FilterCondition>
{
new FilterCondition { PropertyName = "Age", Value = 30, Operator = FilterOperator.GreaterThanOrEqual },
};
var filterBuilder = new DynamicFilterBuilder(typeof(Person));
var filterExpression = filterBuilder.BuildFilterExpression(filterConditions);
var filteredPeople = people.AsQueryable().Where(filterExpression);
foreach (var person in filteredPeople)
{
Console.WriteLine($"Id: {person.Id}, Name: {person.Name}, Age: {person.Age}");
}
}
}
```
In this example, we create a list of `FilterCondition` objects that define the filtering criteria. We then use the `DynamicFilterBuilder` class to build a LINQ expression tree that represents the filtering criteria. Finally, we use the `Where` method to apply the filter to the list of people and print the results.
Enhanced response (with ZPI)
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
public class DynamicFilterBuilder
{
private readonly ParameterExpression _parameter;
public DynamicFilterBuilder(Type entityType)
{
_parameter = Expression.Parameter(entityType);
}
public Expression<Func<object, bool>> BuildFilter(Dictionary<string, object> filterCriteria)
{
Expression body = Expression.Constant(true);
foreach (var criterion in filterCriteria)
{
var propertyInfo = _parameter.Type.GetProperty(criterion.Key);
if (propertyInfo == null)
{
throw new ArgumentException($"Property {criterion.Key} does not exist on the entity type.");
}
var propertyExpression = Expression.Property(_parameter, propertyInfo);
var valueExpression = Expression.Constant(criterion.Value);
var equalityExpression = Expression.Equal(propertyExpression, valueExpression);
body = Expression.AndAlso(body, equalityExpression);
}
return Expression.Lambda<Func<object, bool>>(body, _parameter);
}
}
public class Program
{
public static void Main()
{
var filterBuilder = new DynamicFilterBuilder(typeof(Person));
var filterCriteria = new Dictionary<string, object>
{
{ "Name", "John" },
{ "Age", 30 }
};
var filterExpression = filterBuilder.BuildFilter(filterCriteria);
Console.WriteLine(filterExpression.ToString());
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
```
This implementation provides a `DynamicFilterBuilder` class that can be used to build LINQ expression trees for dynamic filtering. The `BuildFilter` method takes a dictionary of filter criteria, where each key is a property name and each value is the desired value for that property. It then constructs an expression tree that represents the filter criteria.
Here's a step-by-step explanation of how the `BuildFilter` method works:
1. **Initialization**: The method starts by initializing the `body` variable to a constant expression that always evaluates to `true`. This will serve as the base for the filter expression.
2. **Iterating over filter criteria**: The method then iterates over each criterion in the `filterCriteria` dictionary.
3. **Getting property information**: For each criterion, it retrieves the `PropertyInfo` object for the corresponding property on the entity type. If the property does not exist, it throws an `ArgumentException`.
4. **Creating property and value expressions**: It creates expressions for the property and the desired value using `Expression.Property` and `Expression.Constant`, respectively.
5. **Creating equality expression**: It creates an equality expression using `Expression.Equal` to compare the property expression with the value expression.
6. **Combining expressions**: The equality expression is then combined with the existing `body` expression using `Expression.AndAlso`. This effectively adds the new criterion to the filter expression.
7. **Returning the filter expression**: Finally, the method returns the constructed filter expression as a `Func<object, bool>`.
The example usage in the `Main` method demonstrates how to create a `DynamicFilterBuilder` instance for the `Person` class, define filter criteria, and build a filter expression using the `BuildFilter` method. The resulting filter expression can be used to filter a collection of `Person` objects based on the specified criteria.
Grader chain
Each grader reads the same baseline + enhanced pair and casts an independent verdict. Disagreement is the framework's own signal — not noise to be averaged away.
V1claude-haiku-4-5-20251001
WorseV4qwen2.5-coder-7b-instruct
BetterComments
Our grader said what it said. What do you say? Comment as a guest below.
No comments yet. Be the first to say what you make of this round.