EF Classic - LINQ Dynamic - Deferred Methods - ThenByDynamic (EF Core)

CSharp · Console

/Program.cs

// Entity Framework Classic
// Doc: https://entityframework-classic.net/linq-dynamic

// @nuget: Z.EntityFramework.Classic
// @nuget: Newtonsoft.Json

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;  
using Newtonsoft.Json;
using Z.Expressions;

public class Program
{
	public static void Main()
	{
		GenerateData();

		using (var context = new EntityContext())
		{		
			var query = context.Customers.OrderByDynamic(x => "x.Name");
			query = query.ThenByDynamic(x => "x.Description");
			
			FiddleHelper.WriteTable("Customers",query.ToList());		
		}
	}

	public static void GenerateData()
	{
		using (var context = new EntityContext())
		{
			context.Customers.Add(new Customer() { Name = "Customer_A", Description = "ZZZ Projects"});
			context.Customers.Add(new Customer() { Name = "Customer_A", Description = "Description"});			
			context.Customers.Add(new Customer() { Name = "Customer_B", Description = "Description"});
			
			context.SaveChanges();	
		}
	}

	public class EntityContext : DbContext
	{		
		public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
		{		
		}

		public DbSet<Customer> Customers { get; set; }
	}
	
	public class Customer
	{
		public int CustomerID { get; set; }
		public string Name { get; set; }
		public string Description { get; set; }
	}
	
	public class Criteria
	{
		public string Method { get; set; }
		public string Field { get; set; }
		public string Operation { get; set; }
		public string Value { get; set; }
		public string Expression { get; set; }
	}
}
;