static void RunExample() { using (var context = new EFRecipesEntities()) { var cat1 = new Category { Name = "Programming" }; var cat2 = new Category { Name = "Databases" }; var cat3 = new Category { Name = "Operating Systems" }; context.Books.AddObject(new Book { Title = "F# In Practice", Category = cat1 }); context.Books.AddObject(new Book { Title = "The Joy of SQL", Category = cat2 }); context.Books.AddObject(new Book { Title = "Windows 7: The Untold Story", Category = cat3 }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("Books (using LINQ)"); List <string> cats = new List <string> { "Programming", "Databases" }; var books = from b in context.Books where cats.Contains(b.Category.Name) select b; foreach (var book in books) { Console.WriteLine("'{0}' is in category: {1}", book.Title, book.Category.Name); } } using (var context = new EFRecipesEntities()) { Console.WriteLine("Books (using ESQL)"); var esql = @"select value b from Books as b where b.Category.Name in {'Programming','Databases'}"; var books = context.CreateQuery <Book>(esql); foreach (var book in books) { Console.WriteLine("'{0}' is in category: {1}", book.Title, book.Category.Name); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var c1 = new Customer { Name = "Jill Masters", City = "Raytown" }; var c2 = new Customer { Name = "Bob Meyers", City = "Austin" }; var c3 = new Customer { Name = "Robin Rosen", City = "Dallas" }; var o1 = new Order { OrderAmount = 12.99M, Customer = c1 }; var o2 = new Order { OrderAmount = 99.39M, Customer = c2 }; var o3 = new Order { OrderAmount = 101.29M, Customer = c3 }; context.Orders.AddObject(o1); context.Orders.AddObject(o2); context.Orders.AddObject(o3); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("Customers with above average total purchases"); var esql = @"select o.Customer.Name, count(o.OrderId) as TotalOrders, Sum(o.OrderAmount) as TotalPurchases from EFRecipesEntities.Orders as o where o.OrderAmount > anyelement(select value Avg(o.OrderAmount) from EFRecipesEntities.Orders as o) group by o.Customer.Name"; var summary = context.CreateQuery <DbDataRecord>(esql); foreach (var item in summary) { Console.WriteLine("\t{0}, Total Orders: {1}, Total: {2:C}", item["Name"], item["TotalOrders"], item["TotalPurchases"]); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var r1 = new Resume { Title = "C# Developer", Name = "Sally Jones", Body = "...very long resume goes here..." }; context.Resumes.AddObject(r1); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { // using eSQL var query = "select value Recipe8.Resume(r.ResumeId, r.Title, r.Name,'') from Resumes as r"; var result1 = context.CreateQuery <Resume>(query).Single(); Console.WriteLine("Resume body: {0}", result1.Body); context.Resumes.MergeOption = MergeOption.OverwriteChanges; var result2 = context.Resumes.Single(); Console.WriteLine("Resume body: {0}", result2.Body); } using (var context = new EFRecipesEntities()) { // using ExecuteStoreQuery() var result1 = context.ExecuteStoreQuery <Resume>("select ResumeId, Title, Name,'' Body from chapter13.Resume", "Resumes", MergeOption.AppendOnly).Single(); Console.WriteLine("Resume body: {0}", result1.Body); var result2 = context.ExecuteStoreQuery <Resume>("select * from chapter13.Resume", "Resumes", MergeOption.OverwriteChanges).Single(); Console.WriteLine("Resume body: {0}", result2.Body); } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }