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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:39:18+00:00 2026-05-25T21:39:18+00:00

In this query against a datatable i’m trying to do some conditional filtering. The

  • 0

In this query against a datatable i’m trying to do some conditional filtering.
The check against Timeband(index N) should only be done when the Index exists. (the base code only had three item fields, i’ve converted them to a simple list)

Dim res As DataTable =
    (
        From dr As DataRow In dtTimedRow.AsEnumerable()
        Select dr
        Where
        (TimeBands.Count > 0 AndAlso
         dr.Field(Of Short)(fldStarttime) >= TimeBands(0).StartTime
         And dr.Field(Of Short)(fldStarttime) <= TimeBands(0).EndTime
        ) Or
        (TimeBands.Count > 1 AndAlso
         dr.Field(Of Short)(fldStarttime) >= TimeBands(1).StartTime
         And dr.Field(Of Short)(fldStarttime) <= TimeBands(1).EndTime
        ) Or
        (TimeBands.Count > 2 AndAlso
         dr.Field(Of Short)(fldStarttime) >= TimeBands(2).StartTime
         And dr.Field(Of Short)(fldStarttime) <= TimeBands(2).EndTime)
    ).CopyToDataTable()

The above code triggers an Exception if the count = 1. It executes the code next to imeBands.Count > 1 which it should not. What would be the correct solution for this code.

In the mean time i’ve added a simple filter function.

  • 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-25T21:39:18+00:00Added an answer on May 25, 2026 at 9:39 pm

    The problem is that you use And when you should be using AndAlso:

    TimeBands.Count > 2
    AndAlso dr.Field(Of Short)(fldStarttime) >= TimeBands(2).StartTime (= cond1)
    And dr.Field(Of Short)(fldStarttime) <= TimeBands(2).EndTime (= cond2)

    Since And and AndAlso have the same operator precedence, this is evaluated left-to-right as follows:

    (TimeBands.Count > 2 AndAlso cond1) And cond2

    And does not short-circuit, i.e., the right-hand side of the expression (cond2) is always evaulated, yielding an exception when TimeBands.Count <= 2. Change all your Ands to AndAlsos, and you should be fine:

    TimeBands.Count > 2 AndAlso cond1 AndAlso cond2

    An alternative would be to put parenthesis like this:

    TimeBands.Count > 2 AndAlso (cond1 And cond2)

    but that’s just wasting performance. Since cond1 and cond2 don’t have side effects, there’s no reason to avoid short-circuiting. For the same reasons, I recommend to also change your Ors to the short-circuiting variant OrElse.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to execute this query against Oracle linked server, and getting the
I got this error running a query that goes against 2 tables with combined
This query goes against a single table. The table has 60 attributes called attribute1
I'm having a hard time trying to writhe a query against a MS ACCESS
I'm trying to optimize this slow query (>2s) SELECT COUNT(*) FROM crmentity c, mdcalls_trans_activity_update
I need to make a gql query against a set of some objects which
I am running this MySQL query against my db that contains a lot of
Given this linq query against an EF data context: var customers = data.Customers.Where(c =>
This query works great: var pageObject = (from op in db.ObjectPermissions join pg in
This query is related to this one I asked yesterday. I have a radio

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.