I am building a .Net Core 6.0 application that uses Entity Framework.
I have situation where I need to apply the filter(Where clause) based on two properties (eg: Guid? skillType, string skillName). These properties may or may not have values, means that
- Neither of the property will have value
- Either of the property will have value
- Both properties will have value
How do I construct the Where clause? I tried something like this
var paginationListResult = await this.DbContext.Skills .Where(st => skillType == null || st.SkillTypeId == skillType) .Where(st => string.IsNullOrEmpty(skillName) || st.Name == skillName) .Include(sk => sk.SkillType) .Include(sk => sk.Regroupment) .Include(sk => sk.SkillRoleRequirements) .OrderBy(s => s.Name) .ToPaginateListAsync(pageIndex, itemsPerPage, default(CancellationToken)).ConfigureAwait(false);
Is there a better way to handle the situation?