tags:

views:

34

answers:

1

Here is what I am trying:

IQueryable query = this.MyRepository.GetShippingCollection();

IList<SomeListType> myList = query.Where(x => x.Settings
                                      .Where(y => y.SelectorID.Equals(5))
                                      .Count() > 0)
                                   .OrderBy(x => x.Order)
                                   .ToList();

Produces this error: could not resolve property: Settings.ID

If I do it this way it works, but causes over 3,000 queries on my SQL Server:

IList<SomeListType> myList = this.MyRepository.GetShippingCollection().ToList();
myList = myList.Where(x => x.Settings
                        .Where(y => y.SelectorID.Equals(5))
                        .Count() > 0)
               .OrderBy(x => x.Order)
               .ToList();

I know the solution resides within using a "Join".

I have been looking at examples for the last couple hours and can only find Join examples within the Mapping file. I am also finding examples for "ICriteria".

I don't want to have to create seporate entries for all my complex queries in the mapping file so the join within that file will not work.

Since I am using Fluent NHibernate, I am not using "ICriteria". I am using "IQueryable". How do you join multiple tables within "IQueryable"?

Any help with this would be greatly appreciated. Thanks in advance.

+1  A: 

If the second query is executing 3,000 queries, it is almost certainly lazy-loading the Settings collection. As you iterate over the list, you access this collection, and each time NHibernate goes back to the database to fetch it. Try setting the fetch mode for the Settings property to eager load in the mapping.

Beyond that, the LINQ provider could be an issue. What version of NHibernate are you using? The 2.x LINQ provider has some real limitations. It has been reimplemented in the 3.0 trunk, but you'll have to download and compile it from the source.

By the way, ICriteria vs IQueryable is not related to Fluent NHibernate. Criteria API and LINQ are two providers through which you can create queries. Fluent NHibernate is an alternative way to perform configuration.

Jay
This is only a problem on a single page within the Admin.Thanks for your suggestion and in most cases it would be the right move. Unfortunately in my situation it causes more problems then it solves. It causes my other pages to pull too much data from the SQL server. That is bad since SQL is not Localhost.I have tracked down what I have to do. I need to use ICriteria and with the way Fluent NHibernate is setup in the project I am using it is not declared so I can't easily use it. I am working through adding it as an option.Thanks for your suggestion.
Michael D. Kirkpatrick
@Michael Sorry, I wasn't clear. I meant set it to eager load as a way to identify the problem. If that works, then no, you don't want to *always* eager load, only when you need it. You can specify the fetch mode on a per-query basis, but I'm not clear why you can't use the criteria API. It doesn't relate to FNH. If you have an `ISession` with which to do a LINQ query you can do a criteria query.
Jay
I am choosing this as the answer since eager loading solved about 2/3 of the problem.ICriteria was not available because it was not listed in the custom NHibernate Interface. I added it to the custom NHibernate Interface and that option became available. I was able to solve the problem entirely through using ICriteria. Thanks for giving me the idea to look at it in a different light.
Michael D. Kirkpatrick