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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:07:22+00:00 2026-05-12T05:07:22+00:00

I am looking to optimize my LINQ query because although it works right, the

  • 0

I am looking to optimize my LINQ query because although it works right, the SQL it generates is convoluted and inefficient…

Basically, I am looking to select customers (as CustomerDisplay objects) who ordered the required product (reqdProdId), and are registered with a credit card number (stored as a row in RegisteredCustomer table with a foreign key CustId)

var q = from cust in db.Customers
        join regCust in db.RegisteredCustomers on cust.ID equals regCust.CustId
        where cust.CustomerProducts.Any(co => co.ProductID == reqdProdId)
        where regCust.CreditCardNumber != null && regCust.Authorized == true  
        select new  CustomerDisplay
            {
              Id = cust.Id,
              Name = cust.Person.DisplayName,
              RegNumber = cust.RegNumber
            };

As an overview, a Customer has a corresponding Person which has the Name; PersonID is a foreign key in Customer table.
If I look at the SQL generated, I see all columns being selected from the Person table. Fyi, DisplayName is an extension method which uses Customer.FirstName and LastName. Any ideas how I can limit the columns from Person?

Secondly, I want to get rid of the Any clause (and use a sub-query) to select all other CustomerIds who have the required ProductID, because it (understandably) generates an Exists clause.
As you may know, LINQ has a known issue with junction tables, so I cannot just do a cust.CustomerProducts.Products.
How can I select all Customers in the junction table with the required ProductID?

Any help/advice is appreciated.

  • 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-05-12T05:07:23+00:00Added an answer on May 12, 2026 at 5:07 am

    The first step is to start your query from CustomerProducts (as Alex Said):

    IQueryable<CustomerDisplay> myCustDisplay =
        from custProd in db.CustomerProducts
        join regCust in db.RegisteredCustomers 
            on custProd.Customer.ID equals regCust.CustId
        where
            custProd.ProductID == reqProdId
            && regCust.CreditCardNumber != null
            && regCust.Authorized == true
        select new CustomerDisplay
        {
          Id = cust.Id,
          Name = cust.Person.Name,
          RegNumber = cust.RegNumber
        };
    

    This will simplify your syntax and hopefully result in a better execution plan.

    Next, you should consider creating a foreign key relationship between Customers and RegisteredCustomers. This would result in a query that looked like this:

    IQueryable<CustomerDisplay> myCustDisplay =
        from custProd in db.CustomerProducts
        where
            custProd.ProductID == reqProdId
            && custProd.Customer.RegisteredCustomer.CreditCardNumber != null
            && custProd.Customer.RegisteredCustomer.Authorized == true
        select new CustomerDisplay
        {
          Id = cust.Id,
          Name = cust.Person.Name,
          RegNumber = cust.RegNumber
        };
    

    Finally, for optimum speed, have LINQ compile your query at compile time, rather than run time by using a compiled query:

    Func<MyDataContext, SearchParameters, IQueryable<CustomerDisplay>> 
        GetCustWithProd =
        System.Data.Linq.CompiledQuery.Compile(
            (MyDataContext db, SearchParameters myParams) =>
            from custProd in db.CustomerProducts
            where
                custProd.ProductID == myParams.reqProdId
                && custProd.Customer.RegisteredCustomer.CreditCardNumber != null
                && custProd.Customer.RegisteredCustomer.Authorized == true
            select new CustomerDisplay
            {
              Id = cust.Id,
              Name = cust.Person.Name,
              RegNumber = cust.RegNumber
            };
        );
    

    You can call the compiled query like this:

    IQueryable<CustomerDisplay> myCustDisplay = GetCustWithProd(db, myParams);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL query that I am looking to optimize. SELECT * FROM
I'm looking for a way to optimize one SQL query that I have. I'm
I'm just looking to optimize the code below. It works, but I want to
I am looking for some advice how to optimize a couple of SQL stored
Looking to understand what my EXPLAIN results mean here, and to optimize this query
I'm looking to optimize my SQL. My database schema is: HOMES home_id address city
I am looking for a tip on how to optimize this.... SELECT u.uid, u.username,
I'm looking to optimize two queries into one, if possible. My first query searches
I'm looking to optimize a few tables in a database because currently under high
Is there any way to optimize the query below? It requires looking through multiple

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.