نوشتن پرس و جو در Entity Framework با استفاده از LINQ To Entity قسمت سوم
نویسنده: محسن جمشیدی
تاریخ: ۱۳۹۲/۰۴/۲۰ ۱۸:۵۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
private static void Query10()
{
using (var context = new StoreDbContext())
{
var customers = context.Customers;
foreach (var customer in customers)
{
Console.WriteLine("Customer Name: {0}, Customer Family: {1}", customer.Name, customer.Family);
foreach (var order in customer.Orders)
{
Console.WriteLine("\t Order Date: {0}", order.Date);
}
}
}
}private static void Query11()
{
using (var context = new StoreDbContext())
{
var customer = context.Customers.First();
Console.WriteLine("Customer Name: {0}, Customer Family: {1}", customer.Name, customer.Family);
foreach (var order in customer.Orders)
{
Console.WriteLine("\t Order Date: {0}", order.Date);
}
}
}context.Configuration.LazyLoadingEnabled = false;
private static void Query12()
{
using (var context = new StoreDbContext())
{
var customers = context.Customers;
foreach (var customer in customers)
{
Console.WriteLine("Customer Name: {0}, Customer Family: {1}", customer.Name, customer.Family);
foreach (var order in customer.Orders)
{
Console.WriteLine("\t Order Date: {0}", order.Date);
}
}
}
}private static void Query13()
{
using (var context = new StoreDbContext())
{
var customers = context.Customers.Include(c => c.Orders);
//var customers = context.Customers.Include("Orders");
foreach (var customer in customers)
{
Console.WriteLine("Customer Name: {0}, Customer Family: {1}", customer.Name, customer.Family);
foreach (var order in customer.Orders)
{
Console.WriteLine("\t Order Date: {0}", order.Date);
}
}
}context.OrderDetails.Include(o => o.Order.Customer)
context.Orders.Include(o => o.OrderDetail.Select(od => od.Product))
context.Orders.Include(o => o.Customer).Include(o => o.OrderDetail)
private static void Query14()
{
using (var context = new StoreDbContext())
{
var customer = context.Customers.First(c => c.Family == "Jamshidi");
context.Entry(customer).Collection(c => c.Orders).Load();
foreach (var order in customer.Orders)
{
Console.WriteLine(order.Date);
}
}
}private static void Query15()
{
using (var context = new StoreDbContext())
{
var order = context.Orders.First();
context.Entry(order).Reference(o => o.Customer).Load();
Console.WriteLine(order.Customer.FullName);
}
}if (context.Entry(order).Reference(o => o.Customer).IsLoaded)
context.Entry(order).Reference(o => o.Customer).Load();private static void Query16()
{
using (var context = new StoreDbContext())
{
var customer = context.Customers.First(c => c.Family == "Jamshidi");
IQueryable<Order> query = context.Entry(customer).Collection(c => c.Orders).Query();
var order = query.First();
}
}1 | context.Orders.Include(o => o.OrderDetail.Select(od => od.Product)) |
if (context.Entry(order).Reference(o => o.Customer).IsLoaded)
public class Customer
{
public Customer()
{
Orders = new ObservableCollection<Order>();
}
public virtual ICollection<Order> Orders { get; set; }
}
public class Order
{
public virtual Customer Customer { get; set; }
} public virtual ICollection<Order> Orders { get ; set ; }