Database Reference
In-Depth Information
5.
Insert into and query the model using code similar to the pattern shown in Listing 11-12.
Listing 11-12. Using eSQL and LINQ with the VisitSummary() Function to Query the Model
class Program
{
static void Main(string[] args)
{
RunExample();
}
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.Add (p1);
context.Patients.Add (p2);
context.Patients.Add (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 objectContext = (context as IObjectContextAdapter).ObjectContext;
var patients = objectContext.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"));
}
}
 
Search WWH ::




Custom Search