Completed
Last Updated: 17 Oct 2022 11:02 by ADMIN
Release LIB 2022.3.1017 (17 Oct 2022)
A query for all items was sent to the server upon initializing a QueryableEntityCoreCollectionView
Completed
Last Updated: 29 Apr 2022 14:19 by ADMIN
Release LIB 2022.1.502 (2 May 2022)

NullReferenceException is thrown with the following stacktrace when you initialize QueryableEntityCoreCollectionView.

at Telerik.Windows.Data.QueryableProxy`1.get_Count()
   at Telerik.Windows.Data.QueryableEntityCoreCollectionView`1.UpdateTotalItemCount()
   at Telerik.Windows.Data.QueryableCollectionView..ctor(IEnumerable sourceCollection, Type itemType)
   at Telerik.Windows.Data.QueryableCollectionView..ctor(IEnumerable source)
   at Telerik.Windows.Data.QueryableEntityCoreCollectionView`1..ctor(DbContext dbContext, IQueryable`1 query, Collection`1 include)

To work this around, you can create a custom QueryableEntityCoreCollectionView class and override its UpdateTotalItemCount() method.

public class CustomQueryableEntityCoreCollectionView<T> : QueryableEntityCoreCollectionView<T> where T : class, new()
{

	public CustomQueryableEntityCoreCollectionView(DbContext dbContext, IQueryable<T> query, Collection<string> include)
		: base(dbContext, query, include)
	{
	}

	protected override void UpdateTotalItemCount()
	{
		var sourceListProperty = typeof(QueryableCollectionView).GetProperty("SourceList", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
		var sourceList = (IList)sourceListProperty.GetValue(this);
		if (sourceList != null && !(sourceList is ICollectionView))
		{
			this.TotalItemCount = sourceList.Count;
		}
		else
		{
			var proxyProperty = typeof(QueryableEntityCoreCollectionView<T>).GetProperty("Proxy", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
			var proxy = proxyProperty.GetValue(this);
			if (proxy != null)
			{
				var dbSetProperty = proxy.GetType().GetProperty("DbSet");
				var dbSet = dbSetProperty.GetValue(proxy) as DbSet<T>;
				if (dbSet != null)
				{
					this.TotalItemCount = dbSet.Local.Count;
				}
				else
				{
					this.TotalItemCount = this.QueryableSourceCollection.Count();
				}
			}
		}
	}
}