Query sub-object property

Topics: General
Dec 16, 2013 at 9:56 AM
I need to filter content items by different properties. Most of them are in the main part but one is a string property in a sub-object.

I tryied to create a query to accomplish this without luck:
  var usersQuery =
                _orchardServices.ContentManager.Query<UtentePart, UtentePartRecord>();

            if (!string.IsNullOrWhiteSpace(filterUserName)) {
                .Where(x => x.UserName.Contains(filterUserName));
            if (!string.IsNullOrWhiteSpace(filterCustomer))
                usersQuery.Where(x => x.Customer.CompleteName.Contains(filterCustomer));

           var users= usersQuery.List();
Running this code I get:
could not resolve property: Customer.CompleteName of: myProject.Core.Models.UtentePartRecord
"Customer" is a LazyField:
 public class UtentePartRecord : ContentPartRecord
        public virtual CustomerPartRecord Customer { get; set; }

 public class UtentePart : ContentPart<UtentePartRecord>
        private readonly LazyField<CustomerPart> _customer = new LazyField<CustomerPart>();

        public LazyField<CustomerPart> CustomerField { get { return _customer; } }

        public CustomerPart Customer
            get { return _customer.Value; }
            set { _customer.Value = value; }
How can I do this?
Dec 16, 2013 at 10:39 PM
Please show the code that defines the CompleteName property.
Dec 18, 2013 at 12:21 PM
It's a simple string property:
 public class CustomerPart : ContentPart<CustomerPartRecord>
        public virtual string CompleteName
            get { return Record.CompleteName; }
            set { Record.CompleteName = value; }

public class CustomerPartRecord : ContentPartRecord
        public virtual string CompleteName { get; set; }
Dec 19, 2013 at 5:03 AM
In order to limit the number of variables, can you try it without the lazy field?
Dec 19, 2013 at 10:38 AM
I tryied to remove lazy loading but I cannot find the place to load always my Customer.

I found this interesting post:



"I also think that there's a problem with NHibernate not being aware of some of the relationships that are lazy loaded, but I'm not sure. Firstly, I'm wondering if NHibernate is not really able to deduce the true relationship of the lazy-loaded items (by this I mean items that are marked LazyField<T>) because the types don't match up properly. In addition, I think that the way that lazy loading is handled in orchard (at least in the examples I've seen with a delegate in the handler) is inherently going to be in conflict with NHibernate caching in general, as the delegates (I think) will preempt NHibernates native ability to lazy-load which takes advantage of the cache. "

This seems to be my situation.
Dec 20, 2013 at 10:50 AM
Edited Dec 20, 2013 at 2:24 PM
I found the solution following this example:


Now it works!


No... I've to take a step back... It doesn't work! :(