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'm looking to optimize a few tables in a database because currently under high
I am looking to optimize a process that runs continually and makes frequent calls
I am attempting to optimize a process for creating a SQL report based upon
I have a query which takes a long time and I want to optimize
In functional programming, it's often important to optimize any looping code to be tail
I'm looking for an extremely fast atof() implementation on IA32 optimized for US-en locale,
Looking for feedback on : http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools
Looking for an example that: Launches an EXE Waits for the EXE to finish.
Looking for C# class which wraps calls to do the following: read and write
Looking at what's running and nothing jumps out. Thanks!

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.