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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:29:35+00:00 2026-05-15T02:29:35+00:00

I have an arraylist that holds a subset of names found in my database.

  • 0

I have an arraylist that holds a subset of names found in my database. I need to write a query to get a count of the people in the arraylist for certain sections i.e.
There is a field “City” in my database from the people in the arraylist of names I want to know how many of them live in Chicago, how many live in New York etc.

Can someone help me how I might set up an sql statement to handle this. I think somehow I have to pass the subset of names to sql somehow.

Here is a sample how I am writing my sql in my code

Public Shared Function GetCAData(ByVal employeeName As String) As DataTable
        Dim strQuery As String = "SELECT EMPLID, EMPLNME, DISP_TYPE, BEGIN_DTE FROM Corr WHERE (EMPLNME = @name)"
        Dim cmd As New SqlCommand(strQuery)
        cmd.Parameters.Add("@name", SqlDbType.VarChar)
        cmd.Parameters("@name").Value = employeeName
        Dim dt As DataTable = GenericDataAccess.GetData(cmd)
        Return dt
End Function

I need a way to create a function using your sql statement to return a datatable object of names and the parameters would be city, and the List of names.

The above example isnt the sql I am looking for its just a skeleton of what the function would look like that I want to create.

So then you would use the function by iterating through all the cities passing in the same set of names each time in the fron end.

  • 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-15T02:29:36+00:00Added an answer on May 15, 2026 at 2:29 am

    The best approach would depend on how big the subset of names is.

    The very simplest, and probably worst approach would be to just create some dynamic sql like this..

    "select city,count(*) from table where user in ("name1","name2","name3") GROUP BY city"
    

    Where the in section is generated from the arraylist, perhaps like the following

    private string CreateSQL(List<string> names)
    {
        var sb = new StringBuilder();
        sb.Append("select city,count(*) from table where user in ('");
        foreach ( var name in name)
        {
           sb.Append("'");
           sb.Append(name);
           sb.Append("',");
        }
        sb.Remove(sb.Length-1,1); //remove trailing ,
        sb.Append(") GROUP BY city");
    
        return sb.ToString();
    }
    

    The where in clause has a 100 item limit I believe. If your subset is anything like that length, then you really need a better approach, like putting the users in another table and doing a join. In fact if the subset of users is taken from the same database by another query, then post that and we’ll right a single query.

    edit: I actually don’t know how to do where user in type queries as parameterised commands, be wary of sql injection !

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

Sidebar

Related Questions

I have an arraylist that contains items called Room. Each Room has a roomtype
I have an ArrayList<String> that I'd like to return a copy of. ArrayList has
In Java, say you have a class that wraps an ArrayList (or any collection)
I need to make an ArrayList of ArrayLists thread safe. I also cannot have
I have found this example on StackOverflow: var people = new List<Person> { new
I have a ArrayList made up of different elements imported from a db, made
I have an ArrayList<String> , and I want to remove repeated strings from it.
I have some data stored as ArrayList . And when I want to backup
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the

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.