Dynamic LINQ

CSharp · Console

/Program.cs

using System;	
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;

List<Product> products =
            [
                new() { Name = "Laptop",
                        Category = "Electronics",
                        Price = 1200 },
                new() { Name = "Phone",
                        Category = "Electronics",
                        Price = 800 },
                new() { Name = "Desk",
                        Category = "Furniture",
                        Price = 300 },
            ];

// Dynamic filter, sort, and projection
var result = products
    .AsQueryable()
    .Where("Category == @0 && Price >= @1",
           "Electronics",
           900)
    .OrderBy("Price desc")
    .Select("new (Name, Price)")
    .ToDynamicList();

foreach (dynamic item in result)
    Console.WriteLine($"{item.Name} - {item.Price}");

public class Product
{
    public string Name { get; set; }
    public string Category { get; set; }
    public decimal Price { get; set; }
}

// Output: Laptop - 1200
;