concept SQL query in category entity framework

appears as: SQL query, SQL queries
Entity Framework Core in Action

This is an excerpt from Manning's book Entity Framework Core in Action.

This feature makes select-type queries, which this book has shown can produce efficient database queries, into a first-class citizen. You can predefine a LINQ-based query, use an SQL-based query, or you can use a database view and define the SQL query in your database. See https://docs.microsoft.com/en-gb/ef/core/modeling/query-types for more information.

Looking at the SQL code, you can see places to improve it. For example, EF Core doesn’t use the average votes calculated in the SELECT query in the sort or filter, but EF Core repeats the calculation again for the sort (see an example of this problem in section 12.5.6). This is a small thing, but in part 2 of this chapter, which is on replacing the EF Core queries with SQL queries, you’ll correct that, and you can see whether the extra development effort is worth the performance gain.

Listing 13.5 Top-level methods for building and executing the book list query
public static IEnumerable<BookListDto>    #1  
    BookListQuery(this EfCoreContext context,    #2  
        SortFilterPageOptions options)    #3  
{
    var command = BuildQueryString(options, false);    #4  
    using(new LogDapperCommand(command, context))    #5  
    {
        return context.Database.GetDbConnection()    #6  
            .Query<BookListDto>(command, new    #7  
            {    #7  
                pageSize = options.PageSize,    #7  
                skipRows = options.PageSize    #7  
                    * (options.PageNum - 1),    #7  
                filterVal = options.FilterValue    #7  
            });
    }
}
private static string BuildQueryString    #8  
    (SortFilterPageOptions options, bool justCount)    #8  
{
    var selectOptTop = FormSelectPart(options, justCount);    #9  
    var filter = FormFilter(options);    #10  
    if (justCount)    #11  
        return selectOptTop + filter;    #11  
    var sort = FormSort(options);    #12  
    var optOffset = FormOffsetEnd(options);    #13  
    return selectOptTop + filter    #14  
        + sort + optOffset + "\n";    #14  
}

#1   A Dapper query returns an IEnumerable<T> result. By default, it reads all the rows in one go, but you can change Dapper’s buffered options.
#2   Passes in the application’s DbContext, as you’re assuming most of the database accesses will be done via EF Core
#3   Contains the settings of the sort, filter, page controls set by the user.
#4   Calls the method to build the correct query string based on the user options
#5   Just some code to capture the SQL command and how long it took to execute and log it
#6   Gets the type of connection that Dapper needs from the application’s DbContext
#7   Takes the SQL command string and an anonymous class with the variable data.
#8   Combines the parts of the SQL query. Takes in the sort, filter, page options and a boolean if the query is just counting the number of rows.
#9   Forms the Select part: if it’s just for counting, it returns “SELECT COUNT(*) FROM [Books] AS b”; otherwise, it includes all the columns, calculated values, and so on.
#10   Builds the filter, starting with “WHERE ([b].[SoftDeleted] = 0)” and filling in the rest depending on the options
#11   If it’s only a count, you return the SELECT and WHERE parts, because paging needs to total number of rows available.
#12   Adds a sort of the form “ORDER BY [b].[PublishedOn] DESC” or similar
#13   For paging, add an OFFSET value.
#14   Returns the complete SQL command