EF.Plus - Query DbSetFilter - Query Filter By Inheritance/Interface

CSharp · Console

/Program.cs

// @nuget: EntityFramework
// @nuget: Z.EntityFramework.Plus.EF6

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Runtime.Caching;
using Z.EntityFramework.Plus;

public class Program
{
	public static void Main()
	{	
		GenerateData();
				
		using (var context = new EntityContext())
		{		
			// CREATE filter by inheritance
			context.DbSetFilter<Human>(q => q.Where(x => !x.IsHuman));

			// CREATE filter by interface
			context.DbSetFilter<SoftDelete>(q => q.Where(x => x.IsActive));

			// SELECT * FROM Customer Where IsHuman = 0 and IsActive = 1
			FiddleHelper.WriteTable(context.Customers.ToList());
		}
	}
		 
	public static void GenerateData()
	{
		
		using (var context = new EntityContext())
		{
			context.Customers.Add(new Customer() { Name ="Customer_A", IsActive = true, IsHuman = false  });
			context.Customers.Add(new Customer() { Name ="Customer_B", IsActive = true , IsHuman = true });
			context.Customers.Add(new Customer() { Name ="Customer_C", IsActive = false, IsHuman = false });

			context.SaveChanges();	
		}
	}

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

		}
		
		public DbSet<Customer> Customers { get; set; }
	}
	
	public interface SoftDelete
	{
		Boolean IsActive { get; set; }
	}
	
	public interface Human : SoftDelete
	{
		Boolean IsHuman { get; set; }
	}
	
	public class Customer : Human
	{
		public int CustomerID { get; set; }
		public string Name { get; set; }
		public Boolean IsHuman { get; set; }
		public	Boolean IsActive { get; set; }
	}
}
;