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

  • SEARCH
  • Home
  • 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 7688535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:56:37+00:00 2026-05-31T19:56:37+00:00

I have a linq subquery as follows: for (int i = 0; i <

  • 0

I have a linq subquery as follows:

        for (int i = 0; i < parentAttributes.Length; i++)
        {
            Guid parent = parentAttributes[i];
            var subQuery = from sc in db.tSearchCluendexes
                           join a in db.tAttributes on sc.AttributeGUID equals a.GUID
                           join pc in db.tPeopleCluendexes on a.GUID equals pc.AttributeGUID
                           where a.RelatedGUID == parent && userId == pc.CPSGUID                             
                           select sc.CPSGUID;

            query = query.Where(x => subQuery.Contains(x.Id));
        }

The basic idea is that I filter a list of data (as represented by the query variable) based on the results of a subquery executed an indeterminable number of times.

However I now have an issue whereby I only want to execute the where on the query if the subquery actually contains data and I don’t want to execute the subquery within each iteration of the loop (for example by using a Count() or a ToList()) as this will hit the remote resource. Something like below if we take it that a method HasData() actually returns true or false:

        for (int i = 0; i < parentAttributes.Length; i++)
        {
            Guid parent = parentAttributes[i];
            var subQuery = from sc in db.tSearchCluendexes
                           join a in db.tAttributes on sc.AttributeGUID equals a.GUID
                           join pc in db.tPeopleCluendexes on a.GUID equals pc.AttributeGUID
                           where a.RelatedGUID == parent && userId == pc.CPSGUID                             
                           select sc.CPSGUID;

            if (subQuery.HasData())                
               query = query.Where(x => subQuery.Contains(x.Id));
        }

Suggestions are welcome.

  • 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-31T19:56:39+00:00Added an answer on May 31, 2026 at 7:56 pm

    Your suggestions of using Count and ToList are not the fastest way to see if a query has any results. You should use the Any method:

    if (subQuery.Any())
    {            
       query = query.Where(x => subQuery.Contains(x.Id));
    }
    

    Note that this will still be evaluated immediately. If you want to delay execution, you should put it inside the lambda function.

    query = query.Where(x => !subQuery.Any() || subQuery.Contains(x.Id));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have linq query as follows: var result = (from Customer cust in db
I have a Linq to objects statement var confirm = from l in lines.Lines
I have a fairly complex Linq query: var q = from eiods in LinqUtils.GetTable<EIOfficialDesignee>()
I have a query linq like this: var query = from c in Context.Customers
Say that I have LINQ query such as: var authors = from x in
I have an odd linq subquery issue. Given the following data structure: Parents Children
I have LINQ statement that looks like this: return ( from c in customers
I cannot seem to find a way to have LINQ return the value from
I have following LINQ statement: string[] str = { a, ab, abcd }; var
I have this LINQ query and I need to make a second Join to

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.