DeferredSource

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
lsforzini
Posts: 14
Joined: Mon 20 Oct 2008 16:35

DeferredSource

Post by lsforzini » Mon 25 Jan 2010 17:33

I have a method that map an entity from the context to my domain model like this:

public IQueryable ReadAll()
{

IQueryable result = null;
this.Context.ObjectTrackingEnabled = false;
this.Context.DeferredLoadingEnabled = false;
result = from a in this.Context.CATEGORIES
select new DomainModel.CATEGORIES
{
CATEGORYID = a.CATEGORYID,
CATEGORYNAME = a.CATEGORYNAME,
DESCRIPTION = a.DESCRIPTION,
PICTURE = a.PICTURE,
PRODUCTS = (from b in a.PRODUCTS select new DomainModel.PRODUCTS {
DISCONTINUED = b.DISCONTINUED,
PRODUCTID = b.PRODUCTID,
PRODUCTNAME = b.PRODUCTNAME,
QUANTITYPERUNIT = b.QUANTITYPERUNIT,
REORDERLEVEL = b.REORDERLEVEL,
UNITPRICE = b.UNITPRICE,
UNITSINSTOCK = b.UNITSINSTOCK,
UNITSONORDER = b.UNITSONORDER,
}),
};
return result;
}

Why when I do a linq query over the IQueryable I obtain an object of type DomainModel.CATEGORIES with a collection of type Devart.Data.Linq.Provider.DeferredSource insted of type DomainModel.PRODUCTS?
Thanks

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Tue 26 Jan 2010 17:26

This is designed behaviour. It is compatible with Microsoft code. Please refer to this article:
http://msdn.microsoft.com/en-us/library ... abled.aspx

lsforzini
Posts: 14
Joined: Mon 20 Oct 2008 16:35

deferred loading

Post by lsforzini » Fri 29 Jan 2010 13:51

In reference to the previous code, there is a difference in the behavior
between Microsoft linq to sql and yours.
The problem is after executing result.ToList() and disposing context:
if we browse PRODUCTS, provider thows an Exception because want to access database although deferred loading is disabled.

Here is sample code with orders and detail

static void test1(){

string connection = "Password=northwind;User ID=northwind;Data Source=ora10g";
MyOrder alfkiOrders;

using (Northwind.Northwind entities =new Northwind.Northwind(connection)) {

entities.ObjectTrackingEnabled = false;
entities.DeferredLoadingEnabled = false;

// -- dal tier
IQueryable qResult = from o in entities.ORDERs
select new MyOrder {
ORDERID = o.ORDERID,
CUSTOMERID = o.CUSTOMERID,
Detail = from d in o.ORDERDETAILs
select new MyOrderDetail() {
ORDERID=d.ORDERID,
PRODUCTID=d.PRODUCTID,
QUANTITY=d.QUANTITY,

},
};

// -- biz tier
alfkiOrders = qResult.Where(order => order.CUSTOMERID == "ALFKI").First();

}

// accessing alfki order detail
foreach (var item in alfkiOrders.Detail) {
item.QUANTITY=22;
// Error!! IEnumerable execute deferred loading after "First()" or "ToList()" operation
}

}


static void test2() {
// trying with a list
string connection = "Password=northwind;User ID=northwind;Data Source=ora10g";
MyOrder alfkiOrders;

using (Northwind.Northwind entities = new Northwind.Northwind(connection)) {
entities.ObjectTrackingEnabled = false;
entities.DeferredLoadingEnabled = false;

// -- dal tier
IQueryable qResult = from o in entities.ORDERs
select new MyOrder {
ORDERID = o.ORDERID,
CUSTOMERID = o.CUSTOMERID,
Detail = new List(
from d in o.ORDERDETAILs
select new MyOrderDetail() {
ORDERID = d.ORDERID,
PRODUCTID = d.PRODUCTID,
QUANTITY = d.QUANTITY,

}
),
};

// -- biz tier
var allOrders = qResult.ToList(); //ok
alfkiOrders = qResult.Where(order => order.CUSTOMERID == "ALFKI").First(); // error

}

// accessing alfki order detail
foreach (var item in alfkiOrders.Detail) {
item.QUANTITY = 22;
// Error!! IEnumerable execute deferred loading after "First()" or "ToList()" operation
}

}




thanks!

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Mon 01 Feb 2010 09:38

We have replied to you by e-mail.

lsforzini
Posts: 14
Joined: Mon 20 Oct 2008 16:35

Post by lsforzini » Mon 01 Feb 2010 11:42

I don't receive any mail. Could you please resend me? Thanks

Post Reply