Database Reference
In-Depth Information
var p3 = new Product
{
ProductName = "Sundome",
UnitPrice = 49.99M,
Category = c1
};
context.Categories.Add(c1);
context.Products.Add(p1);
context.Products.Add(p2);
context.Products.Add(p3);
var c2 = new Category { CategoryName = "Family Tents" };
var p4 = new Product
{
ProductName = "Evanston",
UnitPrice = 169.99M,
Category = c2
};
var p5 = new Product
{
ProductName = "Montana",
UnitPrice = 149.99M,
Category = c2
};
context.Categories.Add(c2);
context.Products.Add(p4);
context.Products.Add(p5);
context.SaveChanges();
}
// with eSQL
using (var context = new EFRecipesEntities())
{
Console.WriteLine("Using eSQL for the query...");
Console.WriteLine();
string sql = @"Select c.CategoryName, EFRecipesModel
.AverageUnitPrice(c) as AveragePrice from
EFRecipesEntities.Categories as c";
var objectContext = (context as IObjectContextAdapter).ObjectContext;
var cats = objectContext.CreateQuery<DbDataRecord>(sql);
foreach (var cat in cats)
{
Console.WriteLine("Category '{0}' has an average price of {1}",
cat[”CategoryName”], ((decimal)cat["AveragePrice"]).ToString("C"));
}
}
// with LINQ
using (var context = new EFRecipesEntities())
{
Console.WriteLine();
Console.WriteLine("Using LINQ for the query...");
Console.WriteLine();
Search WWH ::




Custom Search