Base solution for your next web application
Open Closed

".where(x=>x....) condition in repository doesn't work #388


User avatar
0
cicciottino created

applying some ".where(x=>....)" condition in the repository the filter is not applied (i verified this with the sqlprofiler as well)

my ApplicationService (PoiAppService):

public class PoiAppService : ApplicationService, IPoiAppService
    {
        private readonly IPoiRepository _poiRepository;
        
        public PoiAppService(IPoiRepository poiRepository)
        {
            _poiRepository = poiRepository;
        }


        public GetPoisOutput GetPois(GetPoisInput input)
        {
            var pois = _poiRepository.GetAllPois(input.State, null);

            return new GetPoisOutput
            {
                Pois = Mapper.Map<List<PoiDto>>(pois)
            };
        }
    }
}

my repository (PoiRepository)

public class PoiRepository : AbpZeroSampleRepositoryBase<Poi,long>, IPoiRepository
    {
        public PoiRepository(IDbContextProvider<AbpZeroSampleDbContext> dbContextProvider)
            : base(dbContextProvider)
        { 
        }


        public List<Poi> GetAllPois(PoiState poistate, PoiType poitype)   //PoiType is not used yet
        {
            var query = GetAll();

            if (poistate == PoiState.NotVerified)
                query.Where(x => x.ValidationRate == 0);      //<--this seems not applied
            if (poistate == PoiState.Verified)
                query.Where(x => x.ValidationRate > 0);	      //<--this seems not applied

            return query.OrderByDescending(x => x.Name).ToList(); 
        }

    }

debugging the code i've noticed that the execution of both (applicationService method and repository method) happens twice. And i've verified with the browser tool that the call to the service happened once! why? the execution passes two times in the same code? anyway the .where condition on the query is never applied (as sqlprofiles has showed me)


6 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    If GetAll with Where does not work, all of our code would not work. There should be a problem in your code or ABP has a huge bug.

    Are you sure you're calling _poiRepository.GetAllPois method. Because you're passing null as second argument ( _poiRepository.GetAllPois(input.State, null)) but it's not nullable on method definition.

    To verify Where, you can just try _poiRepository.GetAll().Where(...some condition...) in the app service.

  • User Avatar
    0
    cicciottino created

    so i've simplified my repo(se below) to restrict the research field but the problem

    persist. the strange thing is that the execution is repeted twice (like in async way)

    my ApplicationService (PoiAppService):

    public class PoiAppService : ApplicationService, IPoiAppService
        {
            private readonly IPoiRepository _poiRepository;
    
            public PoiAppService(IPoiRepository poiRepository)
            {
                _poiRepository = poiRepository;
            }
    
            public GetPoisOutput GetPois()
            {
                var pois = _poiRepository.GetAllPois();
                
                return new GetPoisOutput
                {
                    Pois = Mapper.Map<List<PoiDto>>(pois)
                };
            }
    
        }
    

    my repository (PoiRepository)

    public class PoiRepository : AbpZeroSampleRepositoryBase<Poi, long>, IPoiRepository
        {
            public PoiRepository(IDbContextProvider<AbpZeroSampleDbContext> dbContextProvider)
                : base(dbContextProvider)
            { 
            }
    
            public List<Poi> GetAllPois()
            {
                var query = GetAll();
                query.Where(x=>x.Id == 91);  //an existing id, just to test
                
                return query.OrderByDescending(x => x.Name).ToList(); 
            }
        }
    

    i've notice also that: in the browser's developer tool, the script "GetAll.js" is

    downloaded 2 times, sholud it be unique, really? In an old example project "SimpleTaskProject" i see only one "GetAll.js" (the only

    difference is that my project has ModuleZero installed (from the template) is this script generated on applicationLayer basis? how can i investigate deeply?

    see the image

  • User Avatar
    0
    hikalkan created
    Support Team

    Including script twice is not problem (actually, it's not twice, one for jQuery API, second for AngularJs API). Let's do that: Zip your sample project and send me, so I will test it. Because, I couldn't repeat it. Thanks.

  • User Avatar
    0
    cicciottino created

    thanks a lot, here is my project ready to download (with migrations and seed data) [http://www.filedropper.com/fromtemplate])

    Are there in your example projects some part that uses the javascript function(the ajax

    calls to the services) made for jquery instead of angular? if yes, where? thank 1000 for you kind and active cooperation

    Alessio

  • User Avatar
    0
    guillaumemorin created

    Linq methods, like Where(), do not modify the provided IQueryable object, they return a new IQueryable object, that you must re-assign back into your query variable.

    public class PoiRepository : AbpZeroSampleRepositoryBase<Poi,long>, IPoiRepository
        {
            public PoiRepository(IDbContextProvider<AbpZeroSampleDbContext> dbContextProvider)
                : base(dbContextProvider)
            { 
            }
    
    
            public List<Poi> GetAllPois(PoiState poistate, PoiType poitype)   //PoiType is not used yet
            {
                var query = GetAll();
    
                if (poistate == PoiState.NotVerified)
                    query = query.Where(x => x.ValidationRate == 0);      //<--this seems not applied
                if (poistate == PoiState.Verified)
                    query = query.Where(x => x.ValidationRate > 0);         //<--this seems not applied
    
                return query.OrderByDescending(x => x.Name).ToList(); 
            }
    
        }
    
  • User Avatar
    0
    cicciottino created

    Stupid me :roll: Thanks