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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:13:10+00:00 2026-05-15T15:13:10+00:00

I have a multi-select checkbox. Depending on which one is checked, I want to

  • 0

I have a multi-select checkbox. Depending on which one is checked, I want to combine the results into a single query. Sort of like:

if (Checkbox1.Checked)
{
    var query1 = from t in table1 ...
}

if (Checkbox2.Checked)
{
    var query2 = from t in table2 ...
}

DataGridView1.DataSource = query1.Union(query2); // obviously doesnt
      // work since query1 and query2 are not defined in this scope.

Any idea how to combine these selectively?

  • 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-15T15:13:11+00:00Added an answer on May 15, 2026 at 3:13 pm

    Assuming the queries are of the same type, you could define the queries outside of the conditional statements.

    First, a helper method that creates an empty enumerable of the same type as the parameter:

    static IEnumerable<T> CreateEmptyEnumerable<T>(IEnumerable<T> templateQuery)
    {
        return Enumerable.Empty<T>();
    }
    

    Then, the new code:

    var query1 = from t in table1 ...
    var query2 = from t in table2 ...
    var finalQuery = CreateEmptyEnumerable(query1);
    
    if (Checkbox1.Checked)
    {
        finalQuery = query1;
    }
    
    if (Checkbox2.Checked)
    {
        finalQuery = finalQuery.Union(query2);
    }
    
    DataGridView1.DataSource = finalQuery.ToList(); // to avoid re-enumeration
    

    This performs just fine because the queries aren’t actually executed until they’re enumerated over, as in the call to ToList().

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

Sidebar

Related Questions

I have this HTML code which simulates a dropdown multi-checkbox <div> <div class=select> <span>Select
I have a custom attribute which is multi select type and I want to
I have a multi-table SELECT query which compares column values with itself like below:
I have a multi-select <asp:listbox> and a button. I want to disable the button
I have a multi-select list box. I want to be able to select a
I have multiple multi-select boxes with the same class and i want to unselect
I have a multi-select box which has 2 buttons on the right which are
I have a button, on click of which i display a multi-select dialog. I
I have two multi-select lists in HTML - one of categories, and another of
I have a multi-select listbox which I am binding to a DataTable. DataTable contains

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.