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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:11:08+00:00 2026-06-10T12:11:08+00:00

I have the following Linq to Objects query: var query = this._vaporizerData .Where(row =>

  • 0

I have the following Linq to Objects query:

var query = this._vaporizerData
    .Where(row => row.Coater == coater)
    .Where(row => row.Distributor == distributor)
    .Where(row => row.PowerTubeA.HasValue);

if (query.Any())
{
    tubeA = query
        .Where(row => row.CoaterTime.Value >= readTime.AddMinutes(-5))
        .Where(row => row.CoaterTime.Value <= readTime)
        .OrderByDescending(row => row.CoaterTime)
        .Select(row => row.PowerTubeA)
        .First()
        .Value;
}

I know that when the query.Any() line is executed, the first Linq query is evaluated. My question is this. Let’s assume the results of the first query is 5 rows of data. When I execute the second query (starting with ‘tubeA = query’), am I correct it will evaluate this against just the five rows returned from the first query?

Thanks very much.

  • 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-10T12:11:10+00:00Added an answer on June 10, 2026 at 12:11 pm

    I know that when the query.Any() line is executed, the first Linq query is evaluated.

    Well, sort of. It’s evaluated as far as necessary. So if you have a million rows in _vaporizerData but the first of them matches the filter, it’s not going to iterate over the rest of the data.

    When I execute the second query (starting with ‘tubeA = query’), am I correct it will evaluate this against just the five rows returned from the first query?

    Yes, but it will have to find those rows again, checking the filter against all the rows which didn’t match the first time. So again, if you have a million rows in _vaporizerData but this time only the last row matches the original query, you’ll end up checking against all the 999,999 non-matching rows again.

    Also, if anything else changes the contents of _vaporizerData between the call to Any() and the call to First(), those changes will be visible, as it really is going back to the original data source.

    It would be better to only execute the query once, e.g.

    var minTime = readTime.AddMinutes(-5); // Avoid recalculation
    var result = this._vaporizerData
                     .Where(row => row.Coater == coater)
                     .Where(row => row.Distributor == distributor)
                     .Where(row => row.PowerTubeA.HasValue);
                     .Where(row => row.CoaterTime.Value >= minTime)
                     .Where(row => row.CoaterTime.Value <= readTime)
                     .OrderByDescending(row => row.CoaterTime)
                     .Select(row => row.PowerTubeA)
                     .FirstOrDefault();
    
    if (result != null)
    {
        tubeA = result.Value;
    }
    

    EDIT: Two important points noted in comments:

    • Your original query would throw an exception if we the first query matched an item but the second didn’t; the code above would just end up with result as null. That may or may not be an issue for you.
    • If you use MoreLINQ you could make this more efficient by using .MaxBy(row => row.CoaterTime) instead of using OrderByDescending and First. IIRC, that will fail on an empty input though, so you should only do this if you’re using your original version with First, not the version above.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following LINQ query: var queryGroups = (from p in db.cl_contact_event select
I have the following LINQ expression that's not returning the appropriate response var query
I have the following LINQ query: List<FileInputItem> inputList = GetInputList(); var results = from
i have the following LINQ statement: var query =(from item in _itemRepository.FindAll() where item.Id
I have the following LINQ query: From metasect In DirectCast(Form.ChildDocBox.Tag, META_DOCUMENT).META_SECTIONS From metaset In
I have the following linq query (applied to the Northwind database) (from od in
I have the following LINQ query: List<string> Types = (List<string>)Directory.GetFiles(@C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727) .Where(x => System.IO.Path.GetFileNameWithoutExtension(x).Contains(Microsoft)) .ToList<string>();
I have the following linq expression pulling all data from my database: var items
I'm using LINQ-to-Entities. Using the following query: var x = from u in context.Users
I have this query: var iterator = criteria.binaryAssetBranchNodeIds.GetEnumerator(); iterator.MoveNext(); var binaryAssetStructures = from bas

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.