public class ValidationRule {
public string Description { get; set; }
public string ErrorMessage { get; set; }
public short Id { get; set; }
public bool IsActive { get; set; }
public string Name { get; set; }
public string NewExpression { get; set; }
public string OldExpression { get; set; }
}
public class ValidationRuleDto {
public string ErrorMessage { get; set; }
public string Name { get; set; }
public string NewExpression { get; set; }
public string OldExpression { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
namespace ConsoleApp48DynamicLinq
public class ValidationRule
public string Description { get; set; }
public string ErrorMessage { get; set; }
public short Id { get; set; }
public bool IsActive { get; set; }
public string Name { get; set; }
public string NewExpression { get; set; }
public string OldExpression { get; set; }
public class ValidationRuleDto
public string ErrorMessage { get; set; }
public string Name { get; set; }
public string NewExpression { get; set; }
public string OldExpression { get; set; }
public class Test
public string StageText { get; set; }
internal class Program
static void Main(string[] args)
var old = new Test
StageText = "Not Sold"
var @new = new Test
StageText = "Closed"
var validation = new ValidationRuleDto
OldExpression = "(StageText != \"Closed\")",
NewExpression = "(StageText == \"Closed\")"
var validations = new[] { validation };
ValidationRuleHandler.Validate(old, @new, validations);
public static class ValidationRuleHandler
private static readonly ParsingConfig ParsingConfig = new ParsingConfig
AreContextKeywordsEnabled = false
public static ICollection<string> Validate(
dynamic oldObject,
dynamic newObject,
IEnumerable<ValidationRuleDto> rules) => rules.Select(
var oldResult = new[]
oldObject
}.AsQueryable().Any(ParsingConfig, _.OldExpression);
var newResult = new[]
newObject
}.AsQueryable().Any(ParsingConfig, _.NewExpression);
Console.WriteLine($"Old => {oldObject.StageText} => {_.OldExpression} => {oldResult}");
Console.WriteLine($"New => {newObject.StageText} => {_.NewExpression} => {newResult}");