Reputation: 200
public async Task<IEnumerable<T>> GetAllAsync() {
var dataAsQueryable = repository.GetAllAsQueryable();
if (typeof(TenantEntity).IsAssignableFrom(typeof(T))) {
var tenantId = currentUserService.GetCurrentTenantId();
if (tenantId is null or 0) {
return await dataAsQueryable.ToListAsync();
}
var tenantIdProperty = typeof(T).GetProperty("TenantId", BindingFlags.Public | BindingFlags.Instance);
if (tenantIdProperty != null) {
dataAsQueryable = dataAsQueryable.Where(entity => (long)tenantIdProperty.GetValue(entity) == (long)tenantId);
}
}
return dataAsQueryable;
}
public IQueryable<TEntity> GetAllAsQueryable() { return _dbSet.AsQueryable(); }
TenantId cannot be null in database and type is long. When try to get this query throwing exception like this: Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
Upvotes: 0
Views: 64
Reputation: 1181
This line cannot be translated into a sql call because of the reflection used for accessing the property:
dataAsQueryable = dataAsQueryable.Where(entity => (long)tenantIdProperty.GetValue(entity) == (long)tenantId);
If reflection is required (unlikely), then you'll have to do the filtering outside of the query:
dataAsQueryable = dataAsQueryable.ToArray().Where(entity => (long)tenantIdProperty.GetValue(entity) == (long)tenantId);
I recommend you investigate Expressions to do the filtering which would allow a completely sql-side execution and maximum performance.
Upvotes: 0