Dlinq question

G

Guest

Question concerning deferred evalution. If one has a customer table
and an orders table in the database and the latter table has a customer
id assocatiated with it, when a query is executed like this:

IEnumerable<Customer> customers = from c in db.Customers
where c.CustomerId == customerId
select c

Customer[] arrCust = customers.ToArray<Customer>();
Customer cust = arrCust[0];

All of the orders for the customer are apparently loaded as well. Is
there a way to NOT get all of the orders until a later time, say if you
want just the customer data on one page, and when an orders button is
pressed and another page is requested, then select the orders for that
particular customer in the other page?

If I enumerate the customers collection without executing the query by
calling ToArray and just return the first customer, the orders
collection is still populated.

foreach(Customer c in customers)
{
// For test.
return c;
}

c.Orders collection is still filled.

How is this different from using the Including statement,
customers.Including(c => c.Orders) if they both get all the data
anyways?

Apparently, deferred query execution is not making sense to me because
it looks like nothing is deferred.

Thanks,

Hammad
 
W

William Stacey [MVP]

TMK, the orders are not loaded. They are loaded as a seperate db query when
you access the contained list. Set db.Log = Console.Out to see the
different queries.

--
William Stacey [MVP]

| Question concerning deferred evalution. If one has a customer table
| and an orders table in the database and the latter table has a customer
| id assocatiated with it, when a query is executed like this:
|
| IEnumerable<Customer> customers = from c in db.Customers
| where c.CustomerId == customerId
| select c
|
| Customer[] arrCust = customers.ToArray<Customer>();
| Customer cust = arrCust[0];
|
| All of the orders for the customer are apparently loaded as well. Is
| there a way to NOT get all of the orders until a later time, say if you
| want just the customer data on one page, and when an orders button is
| pressed and another page is requested, then select the orders for that
| particular customer in the other page?
|
| If I enumerate the customers collection without executing the query by
| calling ToArray and just return the first customer, the orders
| collection is still populated.
|
| foreach(Customer c in customers)
| {
| // For test.
| return c;
| }
|
| c.Orders collection is still filled.
|
| How is this different from using the Including statement,
| customers.Including(c => c.Orders) if they both get all the data
| anyways?
|
| Apparently, deferred query execution is not making sense to me because
| it looks like nothing is deferred.
|
| Thanks,
|
| Hammad
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top