TodorMitev

Task3-4

May 15th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace EntityFrameworkHomework
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             ViewCustomersOrders();
  13.             Console.WriteLine("\nWith Native:");
  14.             ViewCustomersOrdersNativeSQL();
  15.         }
  16.  
  17.         private static void ViewCustomersOrdersNativeSQL()
  18.         {
  19.             NorthwindEntities context = new NorthwindEntities();
  20.             string nativeSqlQuery =
  21.                 "SELECT c.ContactName " +
  22.                 "FROM dbo.Customers c ,dbo.Orders o " +
  23.                 "WHERE o.CustomerID = c.CustomerID " +
  24.                 "AND o.ShipCountry='Canada' " +
  25.                 "AND o.OrderDate IN ('1/1/1997','1/1/1998')";
  26.            
  27.             var customers = context.ExecuteStoreQuery<string>(nativeSqlQuery);
  28.  
  29.             foreach (var cust in customers)
  30.             {
  31.                 Console.WriteLine(cust.ToString());
  32.             }
  33.         }
  34.  
  35.         private static void ViewCustomersOrders()
  36.         {
  37.             NorthwindEntities context = new NorthwindEntities();
  38.             DateTime yearBegin = new DateTime(1997, 1, 1);
  39.             DateTime yearFinish = new DateTime(1997, 12, 31);
  40.             var customers =
  41.                 (from o in context.Orders
  42.                  from c in context.Customers
  43.                 where (o.OrderDate >= yearBegin && o.OrderDate <= yearFinish) &&
  44.                        o.ShipCountry == "Canada" && o.CustomerID==c.CustomerID
  45.                 select c.ContactName).Distinct();
  46.            
  47.             foreach (var cust in customers)
  48.             {
  49.                 Console.WriteLine(cust);
  50.             }
  51.            
  52.             /* --With N+1 Query Solving
  53.             var customers =
  54.                 (from o in context.Orders
  55.                  where (o.OrderDate >= yearBegin && o.OrderDate <= yearFinish) &&
  56.                         o.ShipCountry == "Canada"
  57.                  select o.CustomerID).Distinct();
  58.  
  59.             foreach (var c in context.Customers.Include("Orders"))
  60.             {
  61.                 foreach (var item in customers)
  62.                 {
  63.                     if (item == c.CustomerID)
  64.                     {
  65.                         Console.WriteLine(c.ContactName);
  66.                     }
  67.                 }
  68.             }
  69.              */
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment