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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:32:05+00:00 2026-06-08T17:32:05+00:00

Here is a broken down version of my problem : I have a method.

  • 0

Here is a broken down version of my problem :

I have a method. The argument is a list of integers. I wish to create an optimised SQL query that returns a particular value on all rows where the id of that row is equal to one of the integers in the argument list. Simple, right ?

I have a feeling I have made it more difficult than it needs to be :

private List<string> ReturnValue(List<int> ids)
        {
            List<string> ValuesIWantToReturn = new List<string>();
            StringBuilder sb = new StringBuilder("SELECT ValueIWantToReturnfrom table WHERE ");
            foreach (int id in ids)
            {
                sb.Append( string.Format("ID = {0} OR ", id) );
            }
            sb.Remove(sb.Length - 3, 3); //remove trailing "OR"
                           sb.Append(";");
            SqlDataReader reader =  RunSelectQuery( sb.ToString() );
            while (reader.Read())
            {
                ValuesIWantToReturn.Add(reader.GetString(0));
            }

            return ValuesIWantToReturn;
        }

Any feedback on the general readability and structure of my code would be appreciated too. It’s always nice to improve 🙂

  • 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-08T17:32:07+00:00Added an answer on June 8, 2026 at 5:32 pm

    You can use the IN syntax rather than OR

     WHERE id IN (1,2,3,4)
    

    It may be more efficient to pass the list of IDs as a table valued parameter to a stored procedure, and then join that to your table, but that may be over engineering the solution – depending on how long your list of numbers is.

    Instead of your loop, you can use string.Join to build your list

    string query = "SELECT ValueIWantToReturn from table WHERE ID IN ("
        + string.Join(",", ids)
        + ")";
    

    (assuming that there is always at least one number in the list)

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

Sidebar

Related Questions

Have you ever done any .net programming? Yes? Good, here's a massive broken program,
I have a pretty long SQL query that I use to, for all of
The will_paginate gem is broken on my version of Oracle. The default paginate_by_sql method
I am trying to insert php variables into javascript. Here is the broken code:
here's a Fusion Tables map that's broken after a search. It does everything well
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
I've based my work on Phil Haack's article here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx I've also looked over
I have a method in a class for which they are a few different
I have run into a problem attempting to redispatch mouse events in ActionScript 3,

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.