October 18 2025
x => x.Status == 0 && x.LastLogon >= DateTime.Now.AddMonths(-1)"
x => x.Status == 0 && x.LastLogon >= DateTime.Now.AddMonths(-1)
var q = context.Customers.AsQueryable(); if (onlyActive) q = q.Where(x => x.Status == CustomerStatus.IsActive); if (since is not null) q = q.Where(x => x.LastLogon >= since); if (!string.IsNullOrWhiteSpace(search)) q = q.Where(x => x.Name.Contains(search)); var list = await q.OrderBy(x => x.Name).ToListAsync();
// using System.Linq; // using Z.Expressions; string filter = "x => true"; if (onlyActive) filter += " && x.Status == 0"; // assuming enum 0 = IsActive if (since is not null) filter += $@" && x.LastLogon >= DateTime.Parse(""{since:yyyy-MM-dd}"")"; if (!string.IsNullOrWhiteSpace(search)) filter += $@" && x.Name.Contains(""{search}"")"; var list = await context.Customers .WhereDynamic(filter) .OrderBy(x => x.Name) .ToListAsync();
var env = new { IsActive = CustomerStatus.IsActive, LastMonth = DateTime.Now.AddMonths(-1) }; var recentActive = await context.Customers .WhereDynamic(x => "x.Status == IsActive && x.LastLogon >= LastMonth", env) .ToListAsync();
string sort = sortColumn switch { "Name" => "x => x.Name", "LastLogon" => "x => x.LastLogon", "TotalSpent" => "x => x.TotalSpent", _ => "x => x.Name" }; var ordered = await context.Customers .OrderByDynamic(sort) .ThenByDynamic("x => x.CustomerID") .ToListAsync();
// Client picks columns: "CustomerID,Name,Country" var projections = selectedColumns.Split(',') .Select(c => c.Trim()) .Where(c => allowedCols.Contains(c)); var selectExpr = "x => new { " + string.Join(", ", projections.Select(c => $"{c} = x.{c}")) + " }"; var rows = await context.Customers .WhereDynamic("x => x.Status == 0") .SelectDynamic(selectExpr) .ToListAsync();
var one = await context.Customers .FirstOrDefaultDynamic("x => x.Email == \\\"stefan@thecodeman.net\\\" && x.Status == 0");
var env = new { IsActive = CustomerStatus.IsActive, LastMonth = DateTime.Now.AddMonths(-1) }; var result = context.Customers.Execute<IEnumerable>( "Where(x => x.Status == IsActive && x.LastLogon >= LastMonth)" + ".Select(x => new { x.CustomerID, x.Name })" + ".OrderBy(x => x.CustomerID).ToList()", env);
1. Design Patterns that Deliver
This isnât just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 18,000+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 18,000+ subscribers gaining practical tips and resources to enhance your .NET expertise.