Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 696857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:06:29+00:00 2026-05-14T03:06:29+00:00

I just started learning LINQ to SQL, and so far I’m impressed with the

  • 0

I just started learning LINQ to SQL, and so far I’m impressed with the easy of use and good performance.

I used to think that when doing LINQ queries like

from Customer in DB.Customers where Customer.Age > 30 select Customer

LINQ gets all customers from the database (“SELECT * FROM Customers”), moves them to the Customers array and then makes a search in that Array using .NET methods. This is very inefficient, what if there are hundreds of thousands of customers in the database? Making such big SELECT queries would kill the web application.

Now after experiencing how actually fast LINQ to SQL is, I start to suspect that when doing that query I just wrote, LINQ somehow converts it to a SQL Query string

SELECT * FROM Customers WHERE Age > 30

And only when necessary it will run the query.

So my question is: am I right? And when is the query actually run?

The reason why I’m asking is not only because I want to understand how it works in order to build good optimized applications, but because I came across the following problem.

I have 2 tables, one of them is Books, the other has information on how many books were sold on certain days. My goal is to select books that had at least 50 sales/day in past 10 days. It’s done with this simple query:

from Book in DB.Books where (from Sale in DB.Sales where Sale.SalesAmount >= 50 && Sale.DateOfSale >= DateTime.Now.AddDays(-10) select Sale.BookID).Contains(Book.ID) select Book

The point is, I have to use the checking part in several queries and I decided to create an array with IDs of all popular books:

var popularBooksIDs = from Sale in DB.Sales where Sale.SalesAmount >= 50 && Sale.DateOfSale >= DateTime.Now.AddDays(-10) select Sale.BookID;

BUT when I try to do the query now:

from Book in DB.Books where popularBooksIDs.Contains(Book.ID) select Book

It doesn’t work! That’s why I think that we can’t use thins kinds of shortcuts in LINQ to SQL queries, like we can’t use them in real SQL. We have to create straightforward queries, am I right?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:06:29+00:00Added an answer on May 14, 2026 at 3:06 am

    You are correct. LINQ to SQL does create the actual SQL to retrieve your results.

    As for your shortcuts, there are ways to work around the limitations:

    var popularBooksIds = DB.Sales
        .Where(s => s.SalesAmount >= 50 
            && s.DateOfSale >= DateTime.Now.AddDays(-10))
        .Select(s => s.Id)
        .ToList();
    
    // Actually should work. 
    // Forces the table into memory and then uses LINQ to Objects for the query
    var popularBooksSelect = DB.Books
        .ToList()
        .Where(b => popularBooksIds.Contains(b.Id));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning Python... I am writing a simple program that will take
I`m just started learning C#, used to be a VB programmer. In VB.NET, it
i have just started learning linq because i like the sound of it. and
I just started playing with lambdas and Linq expression for self learning. I took
I just started learning SQL a couple of days ago, and I'm trying to
I just started learning Haskell. I think I've got the basics down, but I
I just started learning how to use SwiftMailer and I am having trouble sending
Hi Just started learning c for uni (usually use objective c) and have run
I just started learning Zend (& OO PHP for that matter), I have spent
I've just started learning how to use the Entity Framework to write a very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.