Hier ist eine Lösung ohne for
Schleife, aber leider auch ohne parametrisiertes SQL-Statement:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
public class test {
public static void Main(string[] args)
{
//List<int> listColumns = new List<int>(){ 1, 5, 6, 9};
System.Collections.Generic.List<int> listColumns = new System.Collections.Generic.List<int>(){ 1, 5, 6, 9};
string s = String.Join(", ", listColumns.Select(x => x.ToString()));
string sql = String.Format("SELECT * FROM Table WHERE ID IN ({0})", s);
Console.WriteLine(sql);
}
}
Bitte beachten Sie, dass Sie select *
verwenden wird als schlechte Praxis angesehen
bearbeiten Hier ist etwas neuer Code, da Ihre Liste vom Typ System.Web.UI.MobileControls.List
ist
string sql = String.Format("SELECT * FROM Table WHERE ID IN ({0})",
listColumns.ListItem.Value);
Bearbeiten 2 Ich habe den Code aus deinem Kommentar genommen:
list.Items.Clear();
string values = DropDownList4.SelectedValue;
string[] words = values.Split(',');
foreach (string s in words)
if (s != "" && s != string.Empty && s != null)
list.Items.Add(s);
Ich vermute, dass du das am Dropdown-Ereignis geändert hast oder so? und Ihr Dropdown hat eine Zeichenfolge wie "1,5,6,9" im Wert. Wenn alle meine Annahmen korrekt sind, können Sie Folgendes verwenden:
System.Collections.Generic.List<int> selectedValues = new System.Collections.Generic.List<int>();
foreach (string s in words)
if (!String.IsNullOrWhiteSpace(s))
selectedValues.Add(Convert.ToInt32(s));
string ids = String.Join(", ", selectedValues.Select(x => x.ToString()));
string sql = String.Format("SELECT * FROM Table WHERE ID IN ({0})", ids);