C# Eval - My First Evaluation - Evaluate a C# expression with variables

CSharp · Console

/Program.cs

// C# Eval Expression
// Doc: https://eval-expression.net/my-first-evaluation

// @nuget: Z.Expressions.Eval

/*
## Evaluate a C# expression with variables

In the last example, we created the list dynamically in the `Execute` method and hardcoded the value `2`, which we used to filter. However, those values will be from existing variables and user input in most real-life scenarios.

The `Execute` method takes variables to use in our expression as the second parameter (or all remaining parameters).

There are multiple different ways of passing variables, such as:

- Anonymous Type
- Class Member
- Dictionary
- Expando Object
- Values

In this example, we will create the `list` and `greaterThan` variables and show different ways to pass those variables to our expression.

See more: https://eval-expression.net/my-first-evaluation
*/

using System;
using System.Dynamic;
using System.Collections.Generic;
using Z.Expressions;

public class Program
{
	public static void Main()
	{
		var list = new List<int>() { 1, 2, 3, 4 };
		var greaterThan = 2;

		// Passing parameters with an Anonymous Type
		{
			var rList = Eval.Execute("list.Where(x => x > greaterThan)", new { list, greaterThan });
			
			FiddleHelper.WriteTable((IEnumerable<int>)rList);
		}		

		// Passing parameters with an Anonymous Type using named members
		{
			var rList = Eval.Execute("listName.Where(x => x > greaterThanName)", new { listName = list, greaterThanName = greaterThan });
			
			FiddleHelper.WriteTable((IEnumerable<int>)rList);
		}		

		// Passing parameters with a class instance and using class members
		// NOTE: When only using 1 parameter, you can directly use member names, "Item1" and "Item2" in the case of a Tuple<,>
		{
			var rList = Eval.Execute("Item1.Where(x => x > Item2)", new Tuple<List<int>, int>(list, greaterThan));
			
			FiddleHelper.WriteTable((IEnumerable<int>)rList);
		}		

		// Passing parameters with a Dictionary and using key names
		// NOTE: When only using 1 parameter, you can directly use dictionary key names in the expression
		{
			var dictionary = new Dictionary<string, object>();
			dictionary.Add("list", list);
			dictionary.Add("greaterThan", greaterThan);
			var rList = Eval.Execute("list.Where(x => x > greaterThan)", dictionary);
			
			FiddleHelper.WriteTable((IEnumerable<int>)rList);
		}

		// Passing parameters using an Expando Object and using member names
		// NOTE: When only using 1 parameter, you can directly use member names of the Expando Object
		{
			dynamic expandoObject = new ExpandoObject();
			expandoObject.list = list;
			expandoObject.greaterThan= greaterThan;
			var rList = Eval.Execute("list.Where(x => x > greaterThan)", expandoObject);
			
			FiddleHelper.WriteTable((IEnumerable<int>)rList);
		}

		// Passing parameters directly with the Values
		// NOTE: Because parameters are not named, you need to use the position as our library is not aware of the name "list" and "greaterThan"
		{
			var rList = Eval.Execute("{0}.Where(x => x > {1})", list, greaterThan);

			FiddleHelper.WriteTable((IEnumerable<int>)rList);
		}
	}
}
;