var item = Expression.Parameter(typeof(Product), "item");
var name = Expression.Property(item, nameof(Product.Name));
var price = Expression.Property(item, nameof(Product.Price));
var predicate = Expression.Lambda<Func<Product, bool>>(
Expression.AndAlso(
Expression.Call(name, nameof(string.StartsWith), null, Expression.Constant("A")),
Expression.GreaterThan(price, Expression.Constant(10m))
),
item
);
var productQuery = predicate.Compile();
this.testOutputHelper.WriteLine(productQuery.ToString());
var products = new List<Product>
{
new() { Name = "Abcdef", Price = 5, },
new() { Name = "12345", Price = 15, },
new() { Name = "", Price = 5, },
new() { Name = "Abcdef", Price = 11, },
};
var array = products.Where(productQuery).ToArray();
foreach (var json in array.Select(JsonConvert.SerializeObject))
{
this.testOutputHelper.WriteLine(json);
}
https://www.bilibili.com/video/BV1Lq4y1T7Qs