Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to NHibernate wrapper issue using where statement

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 ?

like image 408
Jacob Avatar asked Nov 06 '22 14:11

Jacob


1 Answers

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.

like image 100
Chris Nicola Avatar answered Nov 15 '22 06:11

Chris Nicola