views:

126

answers:

1

I'am using wrapper to get some data from table User

IQueryable<StarGuestWrapper> WhereQuery =    
session.Linq<User>().Where(u => u.HomeClub.Id == clubId && u.IsActive).Select(
                    u =>
                    new StarGuestWrapper()
                        {
                            FullName = u.Name + " " + u.LastName,
                            LoginTime = u.SomeDateTime,
                            MonthsAsMember = u.SomeIntergerValue,
                            StarRating = u.SomeOtherInteregValue, 
                            UserPicture = u.Photo.PhotoData, 
                            InstructorFullName = u.SomeInstructorName,
                            TalkInteractionDuringSession = u.SomeBoolValue,
                            GoalInteractionDuringSession = u.SomeOtherBoolValue
                        });

I use this without a problem as a IQueryable so I can do useful things before actually running the query. Like :

WhereQuery.Skip(startRowIndex).Take(maximumRows).ToList();

and so on.

The problem occurs using 'where' statement on query. For example:

WhereQuery.Where(s => s.StarRating == 1)

will throw an exception in runtime that 'StarRating' doesn't exist in User table - of course it doesn't it's a wrappers property. It will work if I materialize query by

WhereQuery.AsEnumerable().Where(s => s.StarRating == 1)

but then it loses all the sens of using IQueryable and I don't want to do this.

What is strange and interesting that not all properties from wrapper throw error, all the bool values can be used in where statement. Example :

WhereQuery.Where(s => s.TalkInteractionDuringSession)

It works in EntityFramework , why do I get this error in NHibernate and how to get it working the way I want it to ?

+2  A: 

Keep in mind the older nHibernate Linq provider is only a partial implementation and is no longer being actively worked on. A new and more complete linq provider is being developed now and will be part of NH3.0 (you can checkout the trunk and build it to see if it addresses this problem).

My recommendation is to change your code to call ToList() at the point when you explicitly wish to hit the database. You are passing a future valued query back from your repository at which point anything could technically happen to the query. Even EF and LINQ2SQL cannot translate any possible linq query into SQL.

I do realize this isn't what you want to be able to do, but I think you are trying to bend the framework to do something in a way this isn't very natural at all.

Chris Nicola
Chris, what is the version of last Linq dll? 2.0?
NetSide
I wouldn't know, you would probably have to go ask on the NHUsers list
Chris Nicola
Should probably mention that NH3.0 just hit alpha release and is probably fairly stable if you want to try it out.
Chris Nicola