The Including() operator
Hello,
How would I sub-query the child collection in an including clause to limit the number of child objects that come back.
For example, when I do the following:
from c in Customers
select c).Including(c => (from oin c.Paychecks
where (o.OrderDate >= startDate && o.OrderDate <= endDate)
select o));
An exception is thrown that states "Including expression must refer to an immediate property".
Is there a way to do the above?
The Including operator is buggy...
In any case, I wouldn't worry because they'venuked it from the latest Orcas CTP. My guess is that they're on to a better solution. We'll find out in the March release :)
Joe
The workaround is to project into an anonymous type:
from c in Customers
select new
{
Customer = c,
FilteredOrders = from p in c.Paychecks where ...
}
The Including() operator does not allow you to write a sub-query, only refer to specific properties.
from c in Customers
select c).Including(c => c.Paychecks);
The Including() operator is being removed and replaced with a new feature, DataShape. Instead of per query, you specify a DataShape once for a DataContext. The 'LoadWith' method is equivalent the current 'Including' operator. The 'AssociateWith' method is how you can specify a sub-query for the association.
DataShape shape = new DataShape();
shape.LoadWith<Customer>(c => c.Paychecks);
shape.AssociateWith<Customer>(c => from o in c.Paychecks where o.OrderDate > startDate && o.OrderDate < endDate select o);
I have a problem with this. When Including got a null refrence it didn't crash, now i get a contstraint error. My db model has zero to many references but Linq doesn't get them?
Edit: here is a simple version of my dbmodel:
A -> B = 1 on 1
C -> B = 1 or More
D -> B = 0 or More
Sample code:
var data = new MyDataContext ();
DataShape shape = new DataShape();
shape.LoadWith<A>(a => a.propertyB);
shape.LoadWith<B>(b => b.propertyC);
data.Shape = shape;
A sample = data.A.SingleOrDefault( a => a.id == 1 );
(WORKS)
var data = new MyDataContext ();
DataShape shape = new DataShape();
shape.LoadWith<A>(a => a.propertyB);
shape.LoadWith<B>(b => b.propertyD);
data.Shape = shape;
A sample = data.A.SingleOrDefault( a => a.id == 1 );
(WORKS)
var data = new MyDataContext ();
DataShape shape = new DataShape();
shape.LoadWith<A>(a => a.propertyB);
shape.LoadWith<B>(b => b.propertyC);
shape.LoadWith<B>(b => b.propertyD);
data.Shape = shape;
A sample = data.A.SingleOrDefault( a => a.id == 1 );
(DOESN'T WORK)
Exception: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.