Open Closed

Organization unit code filter issue with linq #9055


0
razkhan78 created

Hi

We are using latest asp.net core jQuery. We need to get list of organization units assigned to user along with child organizationunits.

We have tried below code as explained here https://aspnetboilerplate.com/Pages/Documents/Zero/Organization-Units

var user = await _userManager.GetUserByIdAsync((long)AbpSession.UserId);
var organizationUnits = await _userManager.GetOrganizationUnitsAsync(user);
var organizationUnitCodes = organizationUnits.Select(ou => ou.Code);

organizationUnitList = (from o in _organizationUnitRepository.GetAll()
                        where organizationUnitCodes.Any(code => o.Code.StartsWith(code))
                        select new OrganizationUnit()
                        {
                            Id = o.Id,
                            DisplayName = o.DisplayName,
                            TenantId = o.TenantId
                        });

And getting this error

System.InvalidOperationException: The LINQ expression 'DbSet<OrganizationUnit> .Where(o => __ef_filter__p_0 || !(((ISoftDelete)o).IsDeleted) && __ef_filter__p_1 || ((IMayHaveTenant)o).TenantId == __ef_filter__CurrentTenantId_2) .Where(o => __organizationUnitCodes_0 .Any(code => code == "" || o.Code != null && code != null && o.Code.StartsWith(code)))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information. at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|8_0(ShapedQueryExpression translated, <>c__DisplayClass8_0& ) at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.C...


2 Answer(s)
  • 0
    ismcagdas created
    Support Team

    Hi,

    Can you try this instead;

    var user = await _userManager.GetUserByIdAsync((long)AbpSession.UserId);
    var organizationUnits = await _userManager.GetOrganizationUnitsAsync(user);
    var organizationUnitCodes = organizationUnits.Select(ou => ou.Code).ToList();
    
    var filter = PredicateBuilder.New&lt;OrganizationUnit&gt;();
    
    foreach (var code in organizationUnitCodes)
    {
    	filter = filter.Or(e => e.Code.StartsWith(code));
    }
    
    var organizationUnitList = (from o in _organizationUnitRepository.GetAll().Where(filter)
    	select new OrganizationUnit
    	{
    		Id = o.Id,
    		DisplayName = o.DisplayName,
    		TenantId = o.TenantId
    	}).ToList();
    
  • 0
    razkhan78 created

    Provided solution is working, thanks