Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

ComboBox - How to apply filter in the app service method? #3091


User avatar
0
exlnt created

I am using the standard code provided in the template for rendering dropdowns. I would like to apply a filter to the GetAll() method shown below, so that only "Active" entities from the selected entity are returned.

In all of my entities I use the column "Active".

I would like every combobox method to only return the "Active" records.

public List<ComboboxItemDto> GetCompaniesComboboxItems(int selectedCompany = 0)
  {

       var comps = _companyRepository.GetAll();
        var companyItems = new ListResultDto<ComboboxItemDto>(comps.Select(e => new ComboboxItemDto(e.Id.ToString(), e.CompanyName)).ToList()).Items.ToList();

        var defaultItem = new ComboboxItemDto("", L("NotAssigned"));
         companyItems.Insert(0, defaultItem);

         if (selectedCompany != 0)
         {
             var selectedlistvalues = companyItems.FirstOrDefault(e => e.Value == selectedCompany.ToString());
           if (selectedlistvalues != null)
              {
                 selectedlistvalues.IsSelected = true;
                }           
            }
            else
            {
                defaultItem.IsSelected = true;
            }

            return companyItems;
        }

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

    Hi,

    Why you just don't use:

    var comps = _companyRepository.GetAll().Where(c => c.IsActive).ToList();
    

    If you want that repository automatically does filtering for you, you have two options:

    1. ABP has already a ISoftDelete interface for a similar purpose.
    2. Define your own filter like documented here: <a class="postlink" href="https://www.aspnetboilerplate.com/Pages/Documents/Data-Filters#entity-framework">https://www.aspnetboilerplate.com/Pages ... -framework</a> (if you are using EF 6.x)
  • User Avatar
    0
    exlnt created
    var comps = _companyRepository.GetAll().Where(c => c.IsActive).ToList();
    

    Thanks,,that helps! I had already tried that, but I did not use the .ToList() directly after the .Where.