It is known generics do not support equality comparing if "class" restriction is not applied to a parameter. How can we workaround it for LINQ To Entities?
query.Where(p => p.CategoryId == categoryId); //doesn't work
The workaround I saw before was to use EqualityComparer.Default.Equal, but it can't be translated to SQL.
I also tried to build expression manually:
var parameterExpression = Expression.Parameter(typeof(T));
var categoryIdPropertyExpression = Expression.Property(parameterExpression, "CategoryId");
var categoryIdExpression = Expression.Constant(categoryId);
var equalityExpression = Expression.Equal(categoryIdPropertyExpression, categoryIdExpression);
Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(equalityExpression, parameterExpression);
query = query.Where(lambda);
My T may be long or Nullable. And this code throws an exception if T is Nullable as it can check equality of long and Nullable.
Are there any workarounds that work for EF.