using Z.EntityFramework.Plus;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
using System.Collections.Generic;
private static Guid _fact1;
private static Guid _fact2;
private static Guid _fact3;
public static EntityContext context = new EntityContext();
public static void Main()
context.Database.EnsureCreated();
_fact1 = Guid.Parse("11111111-ABA9-90B8-4C00-37C288761F26");
_fact2 = Guid.Parse("22222222-ABA9-90B8-4C00-37C288761F26");
_fact3 = Guid.Parse("33333333-ABA9-90B8-4C00-37C288761F26");
using (var context = new EntityContext())
List<Fact> fullList = context.Facts.Where(x => x.FactId != Guid.Empty).Include(p => p.Patients).Include(p => p.Persons).Include(p => p.Addresses).ToList();
Console.WriteLine("Full List - {0}", fullList.Count());
foreach (var fact in fullList)
Console.WriteLine("Fact: {0} \tPatients: {1} \tPersons: {2} \tAddresses: {3}", fact.FactId, fact.Patients.Count(), fact.Persons.Count(), fact.Addresses.Count());
using (var context = new EntityContext())
var list = new List<Guid>{_fact1, _fact3};
var doubleFilter = context.Facts.Where(b => list.Contains(b.FactId))
.Include(p => p.Patients)
.Include(p => p.Addresses)
foreach (var fact in doubleFilter)
fact.Patients = fact.Patients.Where(x => x.ValidTo == DateTime.MaxValue ).ToList();
fact.Persons = fact.Persons.Where(x => x.ValidTo == DateTime.MaxValue ).ToList();
fact.Addresses = fact.Addresses.Where(x => x.ValidTo == DateTime.MaxValue).ToList();
Console.WriteLine("Double Filtered List");
foreach (var fact in doubleFilter)
Console.WriteLine("Fact: {0} \tPatients: {1} \tPersons: {2} \tAddresses: {3}", fact.FactId, fact.Patients.Count(), fact.Persons.Count(), fact.Addresses.Count());
using (var context = new EntityContext())
var list = new List<Guid>{_fact1, _fact3};
var doubleFilter = context.Facts.Where(b => list.Contains(b.FactId))
.IncludeFilter(p => p.Patients.Where(f => f.ValidTo == DateTime.MaxValue))
.IncludeFilter(p => p.Persons.Where(f => f.ValidTo == DateTime.MaxValue))
.IncludeFilter(p => p.Addresses.Where(f => f.ValidTo == DateTime.MaxValue))
Console.WriteLine("Double Filtered List - filtred");
foreach (var fact in doubleFilter)
Console.WriteLine("Fact: {0} \tPatients: {1} \tPersons: {2} \tAddresses: {3}", fact.FactId, fact.Patients.Count(), fact.Persons.Count(), fact.Addresses.Count());
public static void GenerateData()
using (var context = new EntityContext())
context.Facts.Add(new Fact()
{FactId = _fact1, ValidTo = DateTime.MaxValue});
context.Facts.Add(new Fact()
{FactId = _fact2, ValidTo = DateTime.Now});
context.Facts.Add(new Fact()
{FactId = _fact3, ValidTo = DateTime.MaxValue});
context.Patients.Add(new Patient()
{PatientId = Guid.NewGuid(), FactId = _fact1, Content = "Patient1 in Fact1", ValidTo = DateTime.MaxValue, });
context.Patients.Add(new Patient()
{PatientId = Guid.NewGuid(), FactId = _fact1, Content = "Patient1 in Fact1", ValidTo = DateTime.Now, });
context.Patients.Add(new Patient()
{PatientId = Guid.NewGuid(), FactId = _fact2, Content = "Patient2 in Fact2", ValidTo = DateTime.MaxValue, });
context.Patients.Add(new Patient()
{PatientId = Guid.NewGuid(), FactId = _fact2, Content = "Patient2 in Fact2", ValidTo = DateTime.Now, });
context.Patients.Add(new Patient()
{PatientId = Guid.NewGuid(), FactId = _fact3, Content = "Patient3 in Fact3", ValidTo = DateTime.MaxValue, });
context.Persons.Add(new Person()
{PersonId = Guid.NewGuid(), FactId = _fact1, Content = "Person1 in Fact1", ValidTo = DateTime.MaxValue, });
context.Persons.Add(new Person()
{PersonId = Guid.NewGuid(), FactId = _fact1, Content = "Person1 in Fact1", ValidTo = DateTime.Now, });
context.Persons.Add(new Person()
{PersonId = Guid.NewGuid(), FactId = _fact2, Content = "Person2 in Fact2", ValidTo = DateTime.Now, });
context.Persons.Add(new Person()
{PersonId = Guid.NewGuid(), FactId = _fact2, Content = "Person2 in Fact2", ValidTo = DateTime.MaxValue, });
context.Persons.Add(new Person()
{PersonId = Guid.NewGuid(), FactId = _fact3, Content = "Person3 in Fact3", ValidTo = DateTime.Now, });
context.Addresses.Add(new Address()
{AddressId = Guid.NewGuid(), FactId = _fact1, Content = "Address1 in Fact1", ValidTo = DateTime.MaxValue, });
context.Addresses.Add(new Address()
{AddressId = Guid.NewGuid(), FactId = _fact1, Content = "Address1 in Fact1", ValidTo = DateTime.Now, });
context.Addresses.Add(new Address()
{AddressId = Guid.NewGuid(), FactId = _fact2, Content = "Address2 in Fact2", ValidTo = DateTime.MaxValue, });
context.Addresses.Add(new Address()
{AddressId = Guid.NewGuid(), FactId = _fact3, Content = "Address3 in Fact3", ValidTo = DateTime.MaxValue, });
context.Addresses.Add(new Address()
{AddressId = Guid.NewGuid(), FactId = _fact3, Content = "Address3 in Fact3", ValidTo = DateTime.Now, });
public class EntityContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
public DbSet<Patient> Patients
public DbSet<Address> Addresses
public DbSet<Person> Persons
public IEnumerable<Patient> Patients
public IEnumerable<Address> Addresses
public IEnumerable<Person> Persons