How can I optimize and simplify my query logic?

I’m working on a method that retrieves a list of Student entities from a database based on several optional filtering and sorting criteria. The query is quite complex due to the following reasons:

Conditional Filtering:

Filtering by DivisionId, UserId, SiteId, StudentCategory, Status, and other fields depending on whether parameters are provided.
Some filters are combined using OR conditions, which complicates readability and maintainability.

Sorting:

Sorting by fields like EnrollmentNumber, StartDate, or Status, with support for ascending/descending order.
For EnrollmentNumber, I have a custom comparison logic that handles prefixed strings like “ABC-123”.

Pagination:

Skipping a specific number of records and limiting the results to a given page size.
Complex Joins and Includes:

The query involves several Include and ThenInclude calls to load related data such as StudentInfo, Statuses, and Enrollment.

My primary concerns are:

Performance: The query could be slow due to the complexity of filtering and sorting.

Readability: The code is getting hard to maintain and understand.

Optimization: I’m uncertain if I’m using the best practices for query building and executing.

public async Task<Result<List<Student>>> GetAll(GetStudentsQueryParametersDto queryParameters)
{
    try
    {
        IQueryable<Student> query = _dbContext
            .Students.Where(s => s.Enrollment != null && s.Enrollment.IsGroupEnrollment == false)
            .Include(s => s.StudentInfo)
            .Include(s => s.Enrollment);

        if (queryParameters.ClassId.HasValue)
        {
            query = query.Where(s =>
                (s.Enrollment != null && s.Enrollment.ClassId == queryParameters.ClassId)
                || (
                    s.StudentInfo != null
                    && s.StudentInfo.StudentCategory != null
                    && queryParameters.StudentCategories.Contains(s.StudentInfo.StudentCategory)
                )
            );
        }

        if (queryParameters.TeacherId.HasValue)
        {
            query = query.Where(s => s.Enrollment != null && s.Enrollment.TeacherId == queryParameters.TeacherId.Value);
        }

        if (queryParameters.SchoolId.HasValue)
        {
            query = query.Where(s => s.StudentInfo != null && s.StudentInfo.SchoolId == queryParameters.SchoolId.Value);
        }

        if (queryParameters.View != null)
        {
            query = FilterByDate(query, queryParameters.View);
        }

        if (queryParameters.EnrollmentType != null)
        {
            query = FilterByEnrollmentType(query, queryParameters.EnrollmentType);
        }

        query = query
            .Include(s => s.StudentInfo)
            .ThenInclude(i => i!.Statuses)
            .ThenInclude(s => s.StatusReason)
            .Include(s => s.StudentInfo)
            .ThenInclude(i => i!.PreferredSchools)
            .Include(s => s.FocusSubjects);

        if (queryParameters.Status.Count != 0)
        {
            query = FilterByStatus(query, queryParameters.Status);
        }

        var studentsList = new List<Student>();
        if (queryParameters.Sort != null && queryParameters.Sort.ToLower() != "enrollmentnumber")
        {
            var sortedQuery = ApplySorting(query, queryParameters.Sort, queryParameters.Direction);

            var paginatedQuery = sortedQuery.Skip(queryParameters.Offset ?? 0).Take(queryParameters.Limit ?? 10);

            studentsList = await paginatedQuery.ToListAsync();
        }
        else
        {
            studentsList = await query.ToListAsync();
            var orderedStudents = SortByEnrollmentNumber(studentsList, queryParameters.Direction);
            studentsList = orderedStudents.Skip(queryParameters.Offset ?? 0).Take(queryParameters.Limit ?? 10).ToList();
        }

        return Result<List<Student>>.Success(studentsList);
    }
    catch (BadRequestException e)
    {
        _logger.LogError(
            e,
            "Failed to get all students with pagination and parameters: {queryParameters}",
            queryParameters
        );
        return Result<List<Student>>.Failure(Errors.BadRequest(e.Message));
    }
    catch (Exception e)
    {
        _logger.LogError(
            e,
            "Failed to get all students with pagination and parameters: {queryParameters}",
            queryParameters
        );
        return Result<List<Student>>.Failure(Errors.InternalServerError);
    }
}

These are the methods used:

private static IQueryable<Student> ApplySorting(IQueryable<Student> query, string? sortField, string? sortDirection)
{
    var isDescending = sortDirection?.Equals("desc", StringComparison.OrdinalIgnoreCase) ?? false;

    return sortField?.ToLower() switch
    {
        "startdate" => isDescending
            ? query.OrderByDescending(s => s.StudentInfo!.StartDate)
            : query.OrderBy(s => s.StudentInfo!.StartDate),
        "status" => isDescending
            ? query.OrderByDescending(s =>
                s.StudentInfo!.Statuses.OrderByDescending(st => st.CreatedAt).FirstOrDefault()!.Status
            )
            : query.OrderBy(s =>
                s.StudentInfo!.Statuses.OrderByDescending(st => st.CreatedAt).FirstOrDefault()!.Status
            ),
        _ => isDescending
            ? query.OrderByDescending(s => s.StudentInfo!.StartDate)
            : query.OrderBy(s => s.StudentInfo!.StartDate)
    };
}

private static IOrderedEnumerable<Student> SortByEnrollmentNumber(
    IEnumerable<Student> studentsList,
    string? queryParametersDirection
)
{
    if (!string.IsNullOrEmpty(queryParametersDirection) && queryParametersDirection.ToLower() == "desc")
    {
        return studentsList.OrderByDescending(s => s.Enrollment!.EnrollmentNumber, new EnrollmentNumberComparer());
    }

    return studentsList.OrderBy(s => s.Enrollment!.EnrollmentNumber, new EnrollmentNumberComparer());
}

public class EnrollmentNumberComparer : IComparer<string>
{
    public int Compare(string? enrollmentNumber1, string? enrollmentNumber2)
    {
        ArgumentNullException.ThrowIfNull(enrollmentNumber1);
        ArgumentNullException.ThrowIfNull(enrollmentNumber2);

        if (enrollmentNumber1 == enrollmentNumber2)
            return 0;

        var enrollmentNumber1Parts = enrollmentNumber1.Split('-');
        var enrollmentNumber2Parts = enrollmentNumber2.Split('-');
        var enrollmentNumber1Prefix = enrollmentNumber1Parts[0];
        var enrollmentNumber2Prefix = enrollmentNumber2Parts[0];

        var prefixComparisonResult = enrollmentNumber1Prefix.CompareTo(enrollmentNumber2Prefix);

        if (prefixComparisonResult != 0)
        {
            return prefixComparisonResult;
        }
        else
        {
            return int.Parse(enrollmentNumber1Parts[1]).CompareTo(int.Parse(enrollmentNumber2Parts[1]));
        }
    }
}

Overall, the approach is good. Conditionally adding filters etc. to the query if they are needed is far, far better than writing the conditional code into the Linq expression itself.

The first improvement to consider for search read operations like this is: “Do I need the entire entity graph?” Most of the time you will have a search result view that displays a row per result possibly with a summary of some related data like a count, or “flattening” information from a related entity. An improvement is to define a summary view model or DTO for the search result containing just the fields you intend to display, then use EF to project to that DTO using .Select(). This results in a more lightweight block of data to read from the server.

If you do need entities then consider just setting up the .Include() statements one time rather than some at the start and then others later on. To reference navigation properties in query expressions you do not need to eager load the entities.

The only real mistake I see is the sorting:

        if (queryParameters.Sort != null && queryParameters.Sort.ToLower() != "enrollmentnumber")
        {
            var sortedQuery = ApplySorting(query, queryParameters.Sort, queryParameters.Direction);

            var paginatedQuery = sortedQuery.Skip(queryParameters.Offset ?? 0).Take(queryParameters.Limit ?? 10);

            studentsList = await paginatedQuery.ToListAsync();
        }
        else
        {
            studentsList = await query.ToListAsync();
            var orderedStudents = SortByEnrollmentNumber(studentsList, queryParameters.Direction);
            studentsList = orderedStudents.Skip(queryParameters.Offset ?? 0).Take(queryParameters.Limit ?? 10).ToList();
        }

… specifically the else condition which is materializing the entire list (bad) before applying a default sort and paginating. This conditional code can be re-factored to:

    bool customSort = queryParameters.Sort != null && queryParameters.Sort.ToLower() != "enrollmentnumber";
    var sortedQuery = customSort 
        ? ApplySorting(query, queryParameters.Sort, queryParameters.Direction);
        : SortByEnrollmentNumber(studentsList, queryParameters.Direction);
    var studentList = await sortedQuery
        .Skip(queryParameters.Offset ?? 0)
        .Take(queryParameters.Limit ?? 10)
        .ToListAsync();

This ensures the materialization does not occur prematurely.

Lastly, when inspecting performance use database analysis tools for query performance against live-like conditions to tune indexing. It likely won’t be possible to create indexes for all scenarios, but look at what is used most often and stands to get the most benefit. Overall the crux of searching is to aim to provide users with what they need rather than the most flexible option you can provide. The more flexibility a search feature provides, the more potential performance issues you can let in if users start abusing the search. Good examples to watch out for are things like using string.Contains() by default for string searches rather than .Equals() or .StartsWith(). More forgiving search options can be provided where they are really needed, but keep those to situations where users explicitly need them, defaulting to more narrow search options being the default.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật