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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:26:27+00:00 2026-05-29T09:26:27+00:00

I am doing a repeated query below, with ‘a’ and ‘b’ incrementing (‘b’ increments

  • 0

I am doing a repeated query below, with ‘a’ and ‘b’ incrementing (‘b’ increments to a limit, then reset and ‘a’ increments). There may be multiple row with given values of ‘a’ and ‘b’.



    struct MyData {int mdA; int mydB; }
    ....

    int find_next_a(int a, int b)
    {
      var q = from p in db.pos
        where (p.a >= a && p.b > b) || (p.a > a && p.b > 0)
        orderby p.a, p.b
        select new MyData
        { 
          mdA = p.a, 
          mdB = p.b 
        };

      return q.First().mdA;   // ERROR: InvalidOperationException at end of data
    }

The query works until I reach the end of the table. Then I get the exception InvalidOperationException. I can’t call q.Count() because I get the same exception.

How can I detect that q has no valid data in it?

[EDIT:]
Thanks Jon Skeet (and Bojan Skrchevski, Bodrick), I post the solution to above.


    int find_next_a(int a, int b)
    {
      var q = from p in db.pos
        where (p.a >= a && p.b > b) || (p.a > a && p.b > 0)
        orderby p.a, p.b
        select new { p.a, p.b };

      var result = q.FirstOrDefault();  // catch running past end of table
      if (result == null)
        return -1; 

      return q.First().a;  // return real data
    }
  • 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-29T09:26:28+00:00Added an answer on May 29, 2026 at 9:26 am

    You can use FirstOrDefault instead:

    var result = q.FirstOrDefault();
    if (result == null)
    {
        // Query results were empty
    }
    

    Note that there’s no way of telling between “a real result which is the default value for the element type” and “no results”… it’s not a problem here, as all your real results are non-null references, but it’s worth being aware of.

    EDIT: I hadn’t noticed that you were using a custom struct; I’d misread it as an anonymous type. I’d strongly recommend using an anonymous type here instead of a mutable struct. Or, given that you only need mdA anyway, you could use:

    var q = from p in db.pos
            where (p.a >= a && p.b > b) || (p.a > a && p.b > 0)
            orderby p.a, p.b
            select (int?) p.a;
    
    int? result = q.FirstOrDefault();
    if (result == null)
    {
        // No results - take whatever action
    }
    else
    {
        // Got a result - find the underlying value...
        int a = result.Value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way of doing this without having the repeated sub queries
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
I have to find recursively if there is any repeated element in an integer
Is there any advantage in doing bitwise operations on word boundaries? Any CPU or
I'm doing a comparison of methods in a construction of an array without repeated
I'm doing a probability calculation. I have a query to calculate the total number
I am doing a query to get Title and RespondBY from the tbl_message table,
Is there a quick way to prevent insertion of repeated data into a table?
I find myself doing this repeatedy. $jq(button).filter(function(){ return this.id.match(/^user_(\d+)_edit$/); }).click(function(){ var matches = this.id.match(/^user_(\d+)_edit$/);
Doing odd/even styling with jQuery is pretty easy: $(function() { $(.oddeven tbody tr:odd).addClass(odd); $(.oddeven

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.