Re: [fluent-nhib] eager loading

78 views
Skip to first unread message

Guido Ziliotti

unread,
Oct 30, 2012, 10:07:13 AM10/30/12
to fluent-n...@googlegroups.com
Why not

.ThenFetch(co => co.Item)) 

?


On Mon, Oct 29, 2012 at 3:44 PM, rema <rema...@yahoo.com> wrote:
I have the following object structure:
 
public class CustomerType
{
public int CustomerTypeId {get; set;}
public string TypeName {get; set;}
}
 
public class Customer
{
public long CustomerId {get; set;}
public CustomerType CustomerType {get; set;}
public CustomerOrder CustomerOrders {get; set;}
}
 
public class CustomerOrder
{
public long CustomerOrderId {get; set;}
public Item Item {get; set;}
}
 
public class Item
{
public int ItemId {get; set;}
public string ItemName {get; set;}
}
 
My Query looks like:
 
            var query =
                session.Query<Customer().Where(cus => cus.CustomerId == customerId)
                .Fetch(c => c.CustomerType)
                .Fetch(c => c.CustomerOrders)
                .ThenFetch(co => co.Select(it => it.Item)).ToList();
 
But I get the following error:
 
"A fetch request must be a simple member access expression; '[100002]' is a SubQueryExpression instead.\r\nParameter name: relatedObjectSelector"
 
If I remove the last .ThenFetch - then it works just fine.
 
I do need to load the Item object
 
Any ideas?
 
Thanks

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/iVb2xQHYlGcJ.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.

rema

unread,
Oct 30, 2012, 10:54:01 AM10/30/12
to fluent-n...@googlegroups.com
.ThenFetch(co => co.Item) <=== At this time co is a list of customer orders so I have to do a select

Guido Ziliotti

unread,
Oct 30, 2012, 11:17:39 AM10/30/12
to fluent-n...@googlegroups.com
Well maybe you should correct the class definition you posted then. I see no list in your class.


To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/8l-F2SL4JDUJ.

rema

unread,
Oct 30, 2012, 11:25:38 AM10/30/12
to fluent-n...@googlegroups.com
Sorry about that. You are correct.
 
The corrected class definition is:
 
public class Customer
{
public long CustomerId {get; set;}
public CustomerType CustomerType {get; set;}
public List<CustomerOrder> CustomerOrders {get; set;}

Guido Ziliotti

unread,
Oct 30, 2012, 11:43:00 AM10/30/12
to fluent-n...@googlegroups.com
Are you looking for this?

var query =
session.Query<Customer().Where(cus => cus.CustomerId == customerId)
.Fetch(c => c.CustomerType)
.FetchMany(c => c.CustomerOrders)
.ThenFetch(co => co.Item)
.ToList()
;


I had no time for setting up your classes, give it a try

To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/VZEjcrCaJJMJ.

rema

unread,
Oct 30, 2012, 12:13:20 PM10/30/12
to fluent-n...@googlegroups.com
That worked. Thanks.
 
However, as my next requirement the class definition has changed to:
 
public class Customer
{
public long CustomerId {get; set;}
public CustomerType CustomerType {get; set;}
public List<CustomerOrder> CustomerOrders {get; set;}
public List<CustomerAddress> CustomerAddresses {get; set;}
}
 
And the issue that I have now run into is that there are 10 customer orders and although there is only one customer address, I get 10 records there.

Joe Brockhaus

unread,
Oct 30, 2012, 12:43:59 PM10/30/12
to fluent-n...@googlegroups.com
I suspect you need a .Distinct()

------
Joe Brockhaus
joe.br...@gmail.com
------------


To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/QMeLpjjRIL8J.

rema

unread,
Oct 30, 2012, 2:14:10 PM10/30/12
to fluent-n...@googlegroups.com
Tried Distinct - still get 10 address records.
Reply all
Reply to author
Forward
0 new messages