Exemplo n.º 1
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                context.Employees.AddObject(new Employee {
                    Name = "Robin Rosen", YearsWorked = 3
                });
                context.Employees.AddObject(new Employee {
                    Name = "John Hancock"
                });
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Employees (using LINQ)");
                var employees = from e in context.Employees
                                select new { Name = e.Name, YearsWorked = e.YearsWorked ?? 0 };
                foreach (var employee in employees)
                {
                    Console.WriteLine("{0}, years worked: {1}", employee.Name, employee.YearsWorked);
                }
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Employees (using ESQL)");
                string esql      = @"select 
                                  e.Name,
                                  case when e.YearsWorked is null then 0
                                       else e.YearsWorked
                                  end as YearsWorked
                                from Employees as e";
                var    employees = context.CreateQuery <DbDataRecord>(esql);
                foreach (var employee in employees)
                {
                    Console.WriteLine("{0}, years worked: {1}", employee.GetString(0), employee.GetInt32(1).ToString());
                }
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Employees (using ESQL w/named constructor)");
                string esql      = @"select value Recipe6.Employee(e.EmployeeId, 
                                  e.Name,
                                  case when e.YearsWorked is null then 0
                                       else e.YearsWorked end) 
                                from Employees as e";
                var    employees = context.CreateQuery <Employee>(esql);
                foreach (var employee in employees)
                {
                    Console.WriteLine("{0}, years worked: {1}", employee.Name, employee.YearsWorked.ToString());
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                string hospital = "Oakland General";
                var    p1       = new Patient {
                    Name = "Robin Rosen", Age = 41
                };
                var p2 = new Patient {
                    Name = "Alex Jones", Age = 39
                };
                var p3 = new Patient {
                    Name = "Susan Kirby", Age = 54
                };
                var v1 = new PatientVisit {
                    Cost = 98.38M, Hospital = hospital, Patient = p1
                };
                var v2 = new PatientVisit {
                    Cost = 1122.98M, Hospital = hospital, Patient = p1
                };
                var v3 = new PatientVisit {
                    Cost = 2292.72M, Hospital = hospital, Patient = p2
                };
                var v4 = new PatientVisit {
                    Cost = 1145.73M, Hospital = hospital, Patient = p3
                };
                var v5 = new PatientVisit {
                    Cost = 2891.07M, Hospital = hospital, Patient = p3
                };
                context.Patients.AddObject(p1);
                context.Patients.AddObject(p2);
                context.Patients.AddObject(p3);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Query using eSql...");
                var esql     = @"Select value ps from EFRecipesEntities.Patients as p join EFRecipesModel.GetVisitSummary() as ps on p.Name = ps.Name where p.Age > 40";
                var patients = context.CreateQuery <VisitSummary>(esql);
                foreach (var patient in patients)
                {
                    Console.WriteLine("{0}, Visits: {1}, Total Bill: {2}",
                                      patient.Name, patient.TotalVisits.ToString(), patient.TotalCost.ToString("C"));
                }
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine();
                Console.WriteLine("Query using LINQ...");
                var patients = from p in context.Patients
                               join ps in context.GetVisitSummary() on p.Name equals ps.Name
                               where p.Age >= 40
                               select ps;
                foreach (var patient in patients)
                {
                    Console.WriteLine("{0}, Visits: {1}, Total Bill: {2}",
                                      patient.Name, patient.TotalVisits.ToString(), patient.TotalCost.ToString("C"));
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }