C# Eval Expression - Dynamic LINQ

CSharp · Console

/Program.cs

using System;
using System.Linq;
					
var products = new[]
        {
            new { Name = "Laptop", Price = 1200, Category = "Electronics" },
            new { Name = "Shirt", Price = 25, Category = "Apparel" },
            new { Name = "Coffee Maker", Price = 85, Category = "Home Appliances" }
        };

// Use Eval to filter dynamically
var filteredProducts = products
            .AsQueryable()
	.WhereDynamic("p => p.Price > 50")
	.OrderByDynamic(p => "p.Name")
	.ToList();

foreach (var product in filteredProducts)
{
	Console.WriteLine($"Name: {product.Name}, Price: {product.Price}, Category: {product.Category}");
}

// Output:
// Name: Coffee Maker, Price: 85, Category: Home Appliances
// Name: Laptop, Price: 1200, Category: Electronics
;