-
Notifications
You must be signed in to change notification settings - Fork 933
NHibernate 5 - IQueryable<T> on collections causes crashes when using AsQueryable #3323
-
NHibernate version: 5.4.2
.net Framework version: 4.8
In previous versions persistent collections did not implement IQueryable and it was possible to use AsQueryable to work with System.Linq.Dynamic using the result.
With NHibernate 5 AsQueryable now creates a linq query provider due to the implementation of IQueryable.
In my case creating the query provider throws a null reference exception and basically breaks everytime when working with System.Linq.Dynamic and persistent collections.
Is there any way to work around this other than not using AsQueryable on persistent collections, I basically just need a IQueryable for an IEnumerable (the actually type is only known at runtime) which worked just fine before?
I noticed there is a way to add a custom query provider using the LinqQueryProviderType but I am not sure if this is really the way to go.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments
-
Would calling .ToArray().AsQueryable()
do the trick in your case?
Without a test case illustrating the issue, it is hard to understand if that may be suitable.
Of course, this solution comes with an overhead: the collection, if not already loaded, will be loaded, and then, already loaded or not, duplicated.
AsQueryable
support has been introduced in NHibernate 5.0 by #375. There are no settings to disable it.
Beta Was this translation helpful? Give feedback.
All reactions
-
Another solution may be to use a custom ICollectionTypeFactory
(see collectiontype.factory_class
setting. That factory would replace the default collection types, allowing you to provide new collection types that would instantiate custom collections, allowing you to override their IQueryable implementation. By example, you could provide your own custom PersistentGenericSet
"disabling" the AsQueryable
feature with something like:
class CustomPersistentGenericSet<T> : PersistentGenericSet<T>, IQueryable<T> { [NonSerialized] IQueryable<T> _queryable; Expression IQueryable.Expression => InnerQueryable.Expression; System.Type IQueryable.ElementType => InnerQueryable.ElementType; IQueryProvider IQueryable.Provider => InnerQueryable.Provider; IQueryable<T> InnerQueryable => _queryable ?? (_queryable = new EnumerableQuery<T>(this)); }
Beta Was this translation helpful? Give feedback.