根据asp.net中的列表值,选择“带Where条件的查询”

本文关键字:Where 条件 查询 选择 net asp 列表 根据 | 更新日期: 2023-09-27 18:27:18

我有一个sql表。我的Asp.Net代码(C#)中也有一个列表。

public static List listColumns = new List();

listColumns随机包含1,2,3…10。例如,它可以包含1、5、6、9。我想在此条件下进行查询。所以,如果列表有这些值,我的查询应该是:

SELECT * FROM Table WHERE id=1 or id=5 or id=6 or id=9

listColumns可能包含不同的值并具有不同的计数。如何定期进行此查询?

根据asp.net中的列表值,选择“带Where条件的查询”

这里有一个没有for循环的解决方案,但不幸的是也没有参数化的SQL语句:

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);
  }
}

请注意,使用select *被认为是一种不好的做法

编辑这是一些新代码,因为您的列表类型为System.Web.UI.MobileControls.List

string sql = String.Format("SELECT * FROM Table WHERE ID IN ({0})", 
                 listColumns.ListItem.Value);

编辑2我已经从您的评论中获取了代码:

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);

我想你在下拉列表中有这个更改事件或其他什么?下拉列表的值中有一个类似"1,5,6,9"的字符串。如果我的所有假设都是正确的,你可以使用:

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);
 //assume a list of int value name listColumns
  var listColumns = new List<int>() { 1, 5, 9 };
  var sb = new StringBuilder();
  sb.Append("SELECT * FROM Table");
   for (int i = 0; i < listColumns.Count; i++)
   {
       if (i == 0)
          {
             sb.Append(" where ");
             sb.Append(" id=" + listColumns[i]);
          }
        else
             sb.Append(" or id=" + listColumns[i]);
    }
   Console.Write(sb.ToString());

将查询变量传递到sqlcommand

//assume input is List of integers 
List<int> ids = new List<int> { 1,5,6,9 };
//build your SQL statement as below
string cmdText = "SELECT * FROM Table  WHERE id IN ({0})";
// add parameters for each id value 
string[] paramNames = ids.Select(
    (s, i) => "@id" + i.ToString()
).ToArray();
string inClause = string.Join(",", paramNames);
//set the parameter values
using (SqlCommand cmd = new SqlCommand(string.Format(cmdText, inClause))) {
    for(int i = 0; i < ids.Count; i++) {
       cmd.Parameters.AddWithValue(paramNames[i], ids[i]);
    }
}

Linq到SQL:

var listColumns = new List<int>() { 1, 5, 6, 9 };
db.Table.Where(x => listColumns.Distinct().Contains(x.id)).ToString();

生成的SQL:

SELECT
    [Extent1].[id] AS [id],
FROM [Table] AS [Extent1]
WHERE [Extent1].[id] IN (1, 5, 6, 9)

编辑

一种产生参数化SQL的方法(如果参数的数量不断变化,这并不是很有用):

listColumns.Distinct().Aggregate(db.Table, (current, c) => current.Where(x => c == x.id)).ToString();