构建SQL"其中"从一行字符串列表声明? [英] Building SQL "where in" statement from list of strings in one line?
问题描述
我有一个List(串),这对应于类型在我们的数据库表。
I have a List(Of String) which corresponds to "types" on our database table.
我们正在使用的DB2 ADO.NET提供者,我的最终查询需要看起来像这样:
We are using the DB2 ADO.NET provider and my final query needs to look something like this:
select * from table where type in (@type1, @type2, @type3, @type4)
在过去,我已经建立的查询参数使用foreach循环/主机变量列表,但我真的想找出一种方法来建立他们一行。当然,我可以参加所有的字符串,但添加了@和递增的数字是给我头疼的问题。
In the past, I've built the list of query parameters / host variables using a ForEach loop, but I would really like to figure out a way to build them in one line. Of course, I can join all of the strings, but adding the "@" and the incrementing digit is giving me headaches.
人对如何做到这一点任何想法?
Anyone have any ideas on how to do this?
推荐答案
会不会像这样工作?
var inList = "(" + string.Join(", ", typeList.Select(t => "@" + t)) + ")";
修改
根据您的意见,这个怎么样?
Edit
Based on your comment, how about this?
var inList = "(" +
string.Join(", ", Enumerable.Range(1, argCount).Select(i +> "@type" + i)) +
")";
这篇关于构建SQL"其中"从一行字符串列表声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!