Documentation for c# Database Queries

Corey Fleig 205 Reputation points
2025-12-15T17:47:14.1666667+00:00

I might be confused about this: I prefer to code DB querying in the standard EF Core

manner, without specifically using LINQ syntax. For example,

var myvar = context.MyTable.Where(p => p.ID == wID);

But if this style is in fact LINQ, then I'll cancel this question. I read somewhere that the LINQ preprocessor just re-writes the lines into EF Core statements, so that's what I'd like to master.

Is there a book or online documentation for low-level querying?

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. AgaveJoe 30,411 Reputation points
    2025-12-15T19:57:09.8766667+00:00

    Let me try to answer your questions and clear up some misunderstandings. First, I assume the context is Entity Framework and working with data from a database.

    The line of code you shared is a LINQ (Language-Integrated Query) method that accepts a lambda expression. LINQ also has SQL-like Query Syntax (from x in y select z) but both are LINQ. Both LINQ method syntax and SQL-like syntax are converted into regular old SQL and sent to the database for processing.

    You can even have different SQL providers which affects the resulting SQL.

    Overview: https://learn.microsoft.com/en-us/dotnet/csharp/linq/

    LINQ Tips: https://www.youtube.com/watch?v=71qFZUyKCy0

    Since you are looking for 'low-level' querying, here is how you can see the SQL being generated:

    Starting with EF Core 5.0, you can call .ToQueryString() directly on any LINQ query variable. This returns the generated SQL string immediately without executing the query against the database.

    You can also configure EF Core to write every SQL statement it executes to your console or debug window. This is done in your DbContext configuration:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Logs SQL to the Console
        optionsBuilder.LogTo(Console.WriteLine);
        
        // OR: Logs SQL to the Visual Studio Debug Output window
        // optionsBuilder.LogTo(msg => System.Diagnostics.Debug.WriteLine(msg));
    }
    
    

    Finally, if you are using Visual Studio, you can hover over a query variable while debugging and expand the DebugView property to see the SQL string without writing any extra code.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2025-12-15T17:47:23.0866667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Entity Framework Core (EF Core) provides extensive documentation and resources for learning how to perform database queries using its features. While EF Core primarily uses Language-Integrated Query (LINQ) for querying data, you can find resources that cover various aspects of querying in EF Core style.

    1. Official Documentation: The EF Core Querying Data documentation provides a comprehensive overview of how to use LINQ with EF Core, including examples of loading data, filtering, and more. This is a great starting point for understanding how to structure your queries.
    2. Books: While specific books on EF Core might vary, many general C# and .NET programming books include sections on EF Core. Look for books that cover ASP.NET Core or data access in .NET, as they often include practical examples of using EF Core for querying.
    3. Tutorials and Samples: The EF Core GitHub repository contains sample projects that demonstrate various querying techniques. These can be very helpful for seeing real-world applications of EF Core querying.
    4. Advanced Scenarios: For more advanced querying techniques, you can explore the advanced scenarios tutorial which includes information on dynamic LINQ and other advanced features.

    By utilizing these resources, you can deepen your understanding of EF Core and improve your database querying skills.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.