C#将list集合转化为sql

2023-02-27 16:24:08 浏览数 (1)

代码语言:javascript复制
将list集合转化为sql in 的范围对象 比如
List<string> idList= new List<string>();
idList.Add(“001”);
idList.Add(“002”);
idList.Add(“003”);

select * from table where id in (‘001’,‘002’,‘001’)

公共方法
/// <summary>
/// C#实现把String字符串转化为SQL语句中的In后接的参数
/// </summary>
/// <param name="aa"></param>
/// <returns></returns>
public static string ToSqlListString(List<string> guidList)
{
StringBuilder sqlString = new StringBuilder();
foreach (string item in guidList)
{
sqlString.AppendFormat("'{0}',", item);
}
string result = sqlString.ToString();
return result.Trim(',');
}

0 人点赞