Write Linq With Eval - Convert string expressions to LINQ expressions dynamically. Supports nullable types, complex expressions, two-parameter lambdas, and comprehensive string operations.
$ dotnet add package Linq.EvalConvert string expressions to LINQ expressions dynamically with full support for nullable types, complex operations, and multi-parameter lambdas.
??, ?. operatorsdotnet add package Linq.Eval
using Linq.Eval;
// Simple property access
var expr1 = "(x,y)=>x.Teacher.Age".ToExpression<Func<Student, Teacher, int?>>();
// Conditional expressions
var expr2 = "(x,y)=>x.Teacher.IsPrinciple ? 0 : 100".ToExpression<Func<Student, Teacher, int>>();
// Null-coalescing operator
var expr3 = "(x,y)=>x.Teacher.Age ?? 0".ToExpression<Func<Student, Teacher, int>>();
// Null-conditional operator
var expr4 = "(x,y)=>x.Teacher?.Age".ToExpression<Func<Student, Teacher, int?>>();
// Complex expressions
var expr5 = "(x,y)=>(x.Teacher?.Age ?? 36) > 35 && (x.Age > 10 || !y.IsPrinciple)".ToExpression<Func<Student, Teacher, bool>>();
// Arithmetic with nullable types
var expr = "x => (x.Salary ?? 0.0) / (double)x.WorkHours > 1000.0"
.ToExpression<Func<Teacher, bool>>();
// String operations
var expr = "x => x.FirstName.Length > 5 && x.LastName.StartsWith(\"S\")"
.ToExpression<Func<Teacher, bool>>();
// Complex nested conditions
var expr = "x => (x.Age ?? 0) > 30 ? (x.Salary ?? 0.0) * 2.0 : (x.Salary ?? 0.0)"
.ToExpression<Func<Teacher, double>>();
// Compare two objects
var expr = "(x, y) => (x.Age ?? 0) < (y.Age ?? 0) ? x.Salary : y.Salary"
.ToExpression<Func<Teacher, Teacher, double?>>();
// Mixed type parameters
var expr = "(x, y) => (double)(x.Age ?? 0) > y ? (x.Salary ?? 0.0) : y"
.ToExpression<Func<Teacher, double, double>>();
See CHANGELOG.md for detailed release notes.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.