using System.Linq.Expressions; using System.Reflection; // I've added a comment TestClassRepo repo = new(); var test = repo.Get(_ => _.Name == "Test" || _.Id == "TestId2"); var testexpression = repo.Get(filter: [ new() { Name = "Test2", Description = "Test2" }]); Console.WriteLine(test); Console.ReadLine(); public class TestClass { public string Name { get; set; } public string Id { get; set; } public string Description { get; set; } public bool? IsActive { get; set; } } public class TestClassRepo { private List _list = [new() { Name = "Test" , Id = "TestId" }, new() { Name = "Test2" , Id = "TestId2", Description = "Test2" }, new() { Name = "Test3" , Id = "Test3" } ]; public List Get(Predicate? predicate = null, List? filter = null) { if (predicate != null) { return TestUtils.Get(predicate, _list); } else if (filter != null) { return TestUtils.GetFilter(filter, _list); } return _list; } } public class TestUtils { public static List Get(Predicate predicate, IEnumerable collection) { List result = []; foreach (var item in collection) { if (predicate(item)) { result.Add(item); } } return result; } public static List GetFilter(List obj, List collection) { ParameterExpression param = Expression.Parameter(typeof(T)); Expression predicate = Expression.Constant(true); foreach (var item in obj) { PropertyInfo[] properties = typeof(T).GetProperties(); foreach (PropertyInfo property in properties) { string name = property.Name; var value = property.GetValue(item); if (value is not null) { MemberExpression propertyExpression = Expression.Property(param, name); ConstantExpression constant = Expression.Constant(value); BinaryExpression body = Expression.Equal(propertyExpression, constant); predicate = Expression.AndAlso(predicate, body); } } } var lambda = Expression.Lambda>(predicate, param); return collection.Where(lambda.Compile()).ToList(); } }