When I set the flag on a navigation property in the suite to indicate that it should be Filterable, this is indeed included in the InputDto, but it is then missing from certain methods in the repository. It only exists on GetListWithNavigationPropertiesAsync and GetCountAsync. However, it should also be present on GetListAsync.
I don’t understand why this filtering option shouldn’t also be available on GetListAsync? What I want to filter should surely be independent of whether or not I want the full set of navigation properties in the result.
We very rarely use the methods that load the navigation properties, as they usually load too much data that we don’t actually need. We’d much rather only load the data we actually need. Nevertheless, we still need the option to filter by an ID (e.g. to load all of a customer’s projects, in which case we’d want to filter by customerId).
We’ve already had to customise the Suite templates because we don’t want paged lists by default, so we’ve implemented GetListAsync and GetPagedListAsync. This works perfectly, and we get the methods generated that we need (perhaps this is something you could consider extending).
I wanted to do the same for the filter, but I got stuck at the ApplyFilter() stage. Everything else is now generated correctly using the navigation properties parameters, but how should I construct the query in ApplyFilter() so that the WhereIf is put together correctly?
This are parts of the changed Server.Repository.EfCoreRepository.txt:
public virtual async Task<List<%%entity-name%%>> GetListAsync(
string? filterText = null,
%%dto-field-names-with-type-include-navigation-properties%%%%<if:HasAnyFilterableProperties>%%,%%</if:HasAnyFilterableProperties>%%
string? sorting = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
CancellationToken cancellationToken = default)
{
var query = ApplyFilter((await GetQueryableAsync()), filterText%%<if:HasAnyFilterableProperties>%%, %%</if:HasAnyFilterableProperties>%%%%dto-field-names-include-navigations%%);
query = query.OrderBy(string.IsNullOrWhiteSpace(sorting) ? %%entity-name%%Consts.GetDefaultSorting(false) : sorting);
return await query.PageBy(skipCount, maxResultCount).ToListAsync(cancellationToken);
}
protected virtual IQueryable<%%entity-name%%> ApplyFilter(
IQueryable<%%entity-name%%> query,
string? filterText = null%%<if:HasAnyFilterableProperties>%%,%%</if:HasAnyFilterableProperties>%%
%%dto-field-names-with-type-include-navigation-properties%%)
{
return query
%%apply-filter-text%%%%np-filter-fields%%
}
Unfortunately, however, the following line does not produce the result I want:
return query %%apply-filter-text%%%%np-filter-fields%%
This is what is generated (doesn't work because of e.LvKapitel or e.Lv):
protected virtual IQueryable<LvProjektGliederung> ApplyFilter(IQueryable<LvProjektGliederung> query, string? filterText = null, Guid? lvId = null, Guid? lvKapitelId = null)
{
return query.WhereIf(!string.IsNullOrWhiteSpace(filterText), e => true).WhereIf(lvId != null && lvId != Guid.Empty, e => e.Lv != null && e.Lv.Id == lvId).WhereIf(lvKapitelId != null && lvKapitelId != Guid.Empty, e => e.LvKapitel != null && e.LvKapitel.Id == lvKapitelId);
}
And this is what I’d like:
protected virtual IQueryable<LvProjektGliederung> ApplyFilter(IQueryable<LvProjektGliederung> query, string? filterText = null, Guid? lvId = null, Guid? lvKapitelId = null)
{
return query.WhereIf(!string.IsNullOrWhiteSpace(filterText), e => true).WhereIf(lvId != null && lvId != Guid.Empty, e => e.LvId == lvId).WhereIf(lvKapitelId != null && lvKapitelId != Guid.Empty, e => e.LvKapitelId == lvKapitelId);
}
Is there a way to solve this via the template?
Thank you.
Adrian
2 Answer(s)
-
0
Yes, this can be solved via the Suite template, but you should not build the filter using
e.NavigationProperty.Id(e.g.e.Lv.Id). In EF Core, filtering through the navigation object can fail (or force unnecessary joins / null checks), especially when the navigation is not included. The correct/robust way is to filter by the foreign key scalar property (e.g.LvId,LvKapitelId) as you expected.What you need to change in your template is the part that generates
%%np-filter-fields%%so it emitse.<ForeignKeyPropertyName> == <param>instead ofe.<NavigationProperty> != null && e.<NavigationProperty>.Id == <param>.
Template approach (what to generate)
Generate this pattern for each filterable navigation property:
.WhereIf(lvId.HasValue && lvId != Guid.Empty, e => e.LvId == lvId) .WhereIf(lvKapitelId.HasValue && lvKapitelId != Guid.Empty, e => e.LvKapitelId == lvKapitelId)So your
ApplyFilterbecomes:protected virtual IQueryable<LvProjektGliederung> ApplyFilter( IQueryable<LvProjektGliederung> query, string? filterText = null, Guid? lvId = null, Guid? lvKapitelId = null) { return query .WhereIf(!string.IsNullOrWhiteSpace(filterText), e => true) .WhereIf(lvId.HasValue && lvId != Guid.Empty, e => e.LvId == lvId) .WhereIf(lvKapitelId.HasValue && lvKapitelId != Guid.Empty, e => e.LvKapitelId == lvKapitelId); }How to implement in Suite templates
- In
Server.Repository.EfCoreRepository.txt,%%np-filter-fields%%is currently producing navigation-based filters. - Update the underlying snippet/macro (where
%%np-filter-fields%%is defined) to prefer the FK property name. Conceptually:- if navigation property is
Lvand the parameter islvId, then emite.LvId == lvId - if navigation property is
Customerand the parameter iscustomerId, emite.CustomerId == customerId
- if navigation property is
This works because ABP Suite-generated entities for 1:N relations normally contain both:
public Guid? LvId { get; set; }public Lv? Lv { get; set; }
So the FK scalar is always available for filtering without loading details.
Notes on “why Suite doesn’t add it to GetListAsync”
ABP Suite’s generated repository pattern typically expects navigation-property-based list methods to be the place where nav-related filtering/joins are applied. But technically, your expectation is valid: filtering by FK should be independent of loading navigation properties, and using FK is the better default for performance and correctness.
Sources
- https://abp.io/docs/10.7/suite/generating-crud-page
- https://abp.io/support/questions/3635
This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post) -
0
However, I need to know what the template for ApplyFilter should look like so that only the FK property name is retrieved. I know that
%%np-filter-fields%%is wrong, but what would be correct?Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)