EF.Plus - Query Filter By Inheritance/Interface (EF Core)

CSharp · Console

/Program.cs

// Entity Framework Plus
// Doc: https://entityframework-plus.net/query-filter

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

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

public class Program
{
	public static void Main()
	{
		using (var context = new EntityContext())
		{
			context.Database.EnsureCreated();
		}
		
		GenerateData();

        using (var context = new EntityContext())
        {
            // CREATE filter by inheritance
            context.Filter<BaseDog>(q => q.Where(x => !x.IsDangerous));

            // CREATE filter by interface
            context.Filter<IAnimal>(q => q.Where(x => x.IsDomestic));

            // SELECT * FROM Cat WHERE IsDomestic = true
            var cats = context.Cats.ToList();

            // SELECT * FROM Dog WHERE IsDomestic = true AND IsDangerous = false
            var dogs = context.Dogs.ToList();
			
			FiddleHelper.WriteTable("Cats", cats);
			FiddleHelper.WriteTable("Dogs", dogs);
        }
    }

    public static void GenerateData()
    {
        using (var context = new EntityContext())
        {
            context.Dogs.Add(new Dog() { Name = "Dog_A", Age = 3, IsDangerous = true, IsDomestic = true });
            context.Dogs.Add(new Dog() { Name = "Dog_B", Age = 7, IsDangerous = false, IsDomestic = true });
            context.Dogs.Add(new Dog() { Name = "Dog_C", Age = 4, IsDangerous = true, IsDomestic = false });

            context.Cats.Add(new Cat() { Name = "Cat_A", Age = 1, IsDomestic = true });
            context.Cats.Add(new Cat() { Name = "Cat_B", Age = 2, IsDomestic = true });
            context.Cats.Add(new Cat() { Name = "Cat_C", Age = 1, IsDomestic = false });

            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<Dog> Dogs { get; set; }
        public DbSet<Cat> Cats { get; set; }
    }

    interface IAnimal
    {
        string Name { get; set; }
        int Age { get; set; }
        bool IsDomestic { get; set; }
    }

    public class BaseDog : IAnimal
    {
        public int DogId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public bool IsDomestic { get; set; }
        public bool IsDangerous { get; set; }
    }

    public class Dog : BaseDog
    { }

    public class Cat : IAnimal
    {
        public int CatId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public bool IsDomestic { get; set; }
    }
}
;