using System.Data.Entity;
using System.Collections.Generic;
public static void Main()
using (var context = new EntityContext())
context.Database.Log = Console.Write;
var customers = context.Customers
.Include(x => x.Invoices)
.WhereDynamic(c => "c.Invoices.Count > 0")
.OrderByDescendingDynamic(c => "c.Invoices.Count")
public static void InsertData()
using (var context = new EntityContext())
context.BulkInsert(CustomerData());
context.BulkInsert(InvoiceData());
public static List<Customer> CustomerData()
List<Customer> list = new List<Customer>()
new Customer() { Name ="John"},
new Customer() { Name ="Andy"},
new Customer() { Name ="Mark"}
public static List<Invoice> InvoiceData()
List<Invoice> list = new List<Invoice>()
new Invoice() { Date = new DateTime(2018,5,3), CustomerID = 1},
new Invoice() { Date = DateTime.Now.AddDays(-5), CustomerID = 1},
new Invoice() { Date = DateTime.Now.AddDays(-3), CustomerID = 1},
new Invoice() { Date = new DateTime(2018,4,15), CustomerID = 2},
new Invoice() { Date = new DateTime(2018,2,20), CustomerID = 3},
new Invoice() { Date = new DateTime(2018,5,22), 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 virtual List<Invoice> Invoices { get; set; }
public int InvoiceID { get; set; }
public DateTime Date { get; set; }
public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }