从字符串列表构建查询
如何获取任意字符串列表(形式为“%[text]%”)和数据库列,并将它们转换为 SQL 查询,对列表中的每个字符串进行 LIKE 比较?
举个例子:我的列表中有三个字符串,“%bc%”、“%def%”和“%ab%”。 这将构建查询:
([ColumnName] LIKE "%bc" AND [ColumnName] LIKE "%def%") AND [ColumnName] LIKE "%ab%"
AC# 示例非常好,但请随意用您选择的语言编写它。
How would you take an arbitrary list of strings (of the form "%[text]%") and a database column, and turn them into a SQL query that does a LIKE comparison for each string in the list?
An example: I have three strings in my list, "%bc%", "%def%" and "%ab%". This builds the query:
([ColumnName] LIKE "%bc" AND [ColumnName] LIKE "%def%") AND [ColumnName] LIKE "%ab%"
A C# example would be excellent, but feel free to write it in the language of your choice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要直接回答您的问题,
要解决您的问题,您应该使用Sql Server全文搜索工具。 查询将是:
有了正确的索引,这应该优于 LIKE 列表
To answer your question directly,
To solve your problem, you should use the Sql Server full-text search tools. the query would be:
With the correct indices, this should outperform a list of LIKEs
我会使用 StringBuilder 和 for 循环。 假设您的列表称为“列表”并且是一个列表:
I'd use a StringBuilder and a for loop. Assuming your list is called "list" and is a List:
它只是地图上的字符串连接:
或
It's just a string join on a map:
or