C# Eval - Linq Dynamic - SelectDynamic with enum

CSharp · Console

/Program.cs

// Entity Framework Plus
// Doc: https://entityframework-plus.net/linq-dynamic

// @nuget: Microsoft.EntityFrameworkCore.SqlServer
// @nuget: Z.EntityFramework.Plus.EFCore
// @nuget: Z.Expressions.Eval

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Z.EntityFramework.Plus;
using Z.Expressions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;

public class Program
{
	public static void Main()
	{
		using (var context = new EntityContext())
		{
			context.Database.EnsureCreated();
		}
		
		GenerateData();
				
		using (var context = new EntityContext())
		{
			FiddleHelper.WriteTable("2 - SelectDynamic", ((IQueryable)context.Customers).SelectDynamic( "x => x.MyEnum").ToListDynamic());		
		}
	}
	
	enum Filter {Customer};
		 
	public static void GenerateData()
	{
		
		using (var context = new EntityContext())
		{
			context.Customers.Add(new Customer() { Name ="Customer_A", IsActive = false, MyEnum = Abc.A });
			context.Customers.Add(new Customer() { Name ="Customer_B", IsActive = true, MyEnum = Abc.B });
			context.Customers.Add(new Customer() { Name ="Customer_C", IsActive = false, MyEnum = Abc.C });

			context.SaveChanges();	
		}
	}

	public class EntityContext : DbContext
	{
		public EntityContext() 
		{
		}

		protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
		{
			optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));

			base.OnConfiguring(optionsBuilder);
		}
		
		public DbSet<Customer> Customers { get; set; }
	}
	
	public enum Abc
	{
		A,
		B,
		C
	}
	
	public class Customer
	{
		public int CustomerID { get; set; }
		public Abc MyEnum { get; set; }
		public string Name { get; set; }
		public Boolean IsActive { get; set; }
	}
}
;