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 8220547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:33:26+00:00 2026-06-07T13:33:26+00:00

I am building a web application which searches its SQL database for the query

  • 0

I am building a web application which searches its SQL database for the query entered by the user. A simple search engine.
Lets propose I have plenty of rows – above a couple of thousands. I want to make the call efficient.
I know that my call is possible with only one* SQL query. I would like to know if Linq-To-Sql does this type of optimization.
I am aware the it uses Lazy for its queries. I cannot benchmark it now because I don’t have enought data on my database.

    foreach(var word in words)
    {
        var niqquh = number == 666 ? "" : Languages[number];
        var S = db.Uploads.Where(w => number == 666 ? true : (w.Language == niqquh));
        if(S.Count() > 20)
            S = S.Take(20);
        S = S.OrderBy(u => (u.Tags.Contains(word) ? 15 : 0) + (u.Name.Contains(word) ? 10 : 0) + (u.Description.Contains(word) ? 5 : 0));
        listOfList.Add(S.ToList());
    }

As you see here, I have the ‘magic number’ 666 which is used to say “whatevery language is ok”.
Then, I want to take only the top 20 elements, so I call Count() and then Take.
Then, I sort the results by my needs.
The point is, that I think that the lazy expression gets evaluated at Count – therefore it cannot be optimized to contain only the top 20 rows at the SQL Query level – but on the client’s (ASP.NET) level. therefore, I basically download the whole database to the client, which is terrible.

Does it get optimized or not, if it doesn’t, how can I do it in an efficient way, even if I have to go back to plain SQL string statements?

*A query for each word

  • 1 1 Answer
  • 0 Views
  • 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-06-07T13:33:29+00:00Added an answer on June 7, 2026 at 1:33 pm
    if(S.Count() > 20) 
    

    This runs a query, yes…

    SELECT COUNT(*) FROM
    

    So you didn’t download the whole table.


    S = S.Take(20);
    

    This does not execute the query. It does limit the (unordered) query to 20 items. Generally you want to apply order before you limit the result.

    There is no exception if you Take more than is there. You’ll just get fewer items. Because of this, the Count is not needed.


    S.ToList();
    

    This runs a query.


    IQueryable<Upload> query = db.Uploads;
    if (number != 666)
    {
       var niqquh = Languages[number];
       query = query.Where(w => w.Language == niqquh);
    }
    query = query
      .OrderBy(u => (u.Tags.Contains(word) ? 15 : 0) + (u.Name.Contains(word) ? 10 : 0) + (u.Description.Contains(word) ? 5 : 0))
      .Take(20);
    listOfList.Add(query.ToList()); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a cryptographic web application, which allows user to sign and encrypt
I am building a local intranet web application which uses the current user's AD
I'm building a web application which has some processing of user requests involved. A
I'm building a web application in which I need to scan the user-uploaded files
I'm building an ajax based web application which lets Facebook users who login to
I am building a web application which will generate XML from a database based
I'm looking for some opinions here, I'm building a web application which has the
I’m building a J2EE web application which uses Oracle SSO with an OID back-end
I'm building a web based application to determine a value, which consists the calculation
I'm building an ASP .NET 2.0 (C#) based web application, which is primarily intended

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.