using System.Data.Entity;
using System.Collections.Generic;
public static void Main()
using (var context = new EntityContext())
context.BulkInsert(CustomerData());
context.BulkInsert(InvoiceData());
using (var context = new EntityContext())
var fromDate = DateTime.Now.AddDays(-7).Date;
var customer = context.Customers.Where(c => c.CustomerID == 1)
.Include(c => c.Invoices)
.Where(c => c.Invoices.Any(i => i.InvoiceDate.Date >= fromDate))
FiddleHelper.WriteTable(customer.Invoices);
public static List<Customer> CustomerData()
List<Customer> list = new List<Customer>()
new Customer() { Name ="Customer_1"},
new Customer() { Name ="Customer_2"},
new Customer() { Name ="Customer_3"}
public static List<Invoice> InvoiceData()
List<Invoice> list = new List<Invoice>()
new Invoice() { InvoiceDate = new DateTime(2018,1,5), CustomerID = 1},
new Invoice() { InvoiceDate = new DateTime(2018,5,3), CustomerID = 1},
new Invoice() { InvoiceDate = DateTime.Now.AddDays(-5), CustomerID = 1},
new Invoice() { InvoiceDate = DateTime.Now.AddDays(-3), CustomerID = 1},
new Invoice() { InvoiceDate = new DateTime(2018,4,15), CustomerID = 2},
new Invoice() { InvoiceDate = new DateTime(2018,2,20), CustomerID = 3},
new Invoice() { InvoiceDate = new DateTime(2018,5,22), CustomerID = 3},
new Invoice() { InvoiceDate = new DateTime(2018,5,23), CustomerID = 3}
public class EntityContext : DbContext
public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
public DbSet<Customer> Customers { get; set; }
public DbSet<Invoice> Invoices { get; set; }
public int CustomerID { get; set; }
public string Name { get; set; }
public List<Invoice> Invoices { get; set; }
public int InvoiceID { get; set; }
public DateTime InvoiceDate { get; set; }
public int CustomerID { get; set; }
public Customer Customer { get; set; }