Linq to sql 查询多列 - 查询帮助
我的屏幕上有一个文本字段,用户可以从一个输入字段中搜索一些内容:
- 地址
- 邮政编码
- 客户名称
- ID
编写查询这些列的查询的最佳方法是什么?
我应该如何接受输入,我应该在空格上分割字符串吗?还是逗号?
用户输入= 67 pottors Lane 99
其中“67 pottors Lane”是地址
“99”是/可能是 ID
我到目前为止所拥有的:
//q = querystring
//filteredlist = extension method for the table that I am querying
//clientlist = list of clients I have passed in as a param
//这是我的查询
if (!string.IsNullOrEmpty(q))
{
var result = q.Trim();
filteredlist = filteredlist
.Where(x => x.ID.Contains(q) || x.SubjectPropertyAddress1.Contains(q)
|| x.SubjectPropertyPostCode.Contains(q)
|| clientlist.Any(y=> x.ClientID == y.ClientID && (y.ID.Contains(q) || y.Name.Contains(q)) ));
}
注意:我将利用 sql 进行索引。
I have a text field on my screen where users can search for a few things from one input field:
- Address
- Postcode
- Client name
- ID
What is the best way to write a query that will query these columns?
How should I take the input in, should I split string on space? or comma?
User input = 67 pottors lane 99
where "67 pottors lane" is address
"99" is/could be ID
What I have so far:
//q = querystring
//filteredlist = extension method for the table that I am querying
//clientlist = list of clients I have passed in as a param
//Here is my query for
if (!string.IsNullOrEmpty(q))
{
var result = q.Trim();
filteredlist = filteredlist
.Where(x => x.ID.Contains(q) || x.SubjectPropertyAddress1.Contains(q)
|| x.SubjectPropertyPostCode.Contains(q)
|| clientlist.Any(y=> x.ClientID == y.ClientID && (y.ID.Contains(q) || y.Name.Contains(q)) ));
}
NOTE: I will make use of indexing using sql.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许一个想法:在数据库中创建一个额外的列,其中包含您想要搜索的所有列作为一个大的串联字符串。然后对输入字符串与数据库中的该字段进行自由文本搜索。
由于 L2SQL 不支持自由文本搜索,因此在这种情况下您需要在存储过程中执行此操作,或者像这里一样(http://stackoverflow.com/questions/67706/linqtosql-and-full-text-search-can-it -完成)
Perhaps an idea: make an extra column in your database with all the columns you want to search for as one big concattenated string. Then do a free text search of your input string versus that field in the DB.
Since L2SQL does not support free text search, you need to do it in stored procedure in that case or do like here (http://stackoverflow.com/questions/67706/linqtosql-and-full-text-search-can-it-be-done)
哎呀 - 有没有办法将输入拆分为 ID、地址、邮政编码和姓名的单独字段?
如果是这样,您可以继续附加 Where 子句,例如
.. 与名称、地址等相同。
否则,您的查询更像是搜索引擎查询,而不是 RDBMS 查询。
Ouch - is there no way for you to split the input into separate fields for ID, Address, Postcode and Name?
If so, you can keep appending Where clauses, like
.. same for Name, address etc
Otherwise, your query resembles more of a search engine query than a RDBMS query.
我过去也做过类似的事情。我将搜索字符串拆分为空格字符,并将其限制为最多 6 个搜索“单词”,以便 linq 仍然易于管理。
我想出了如下的方法:
I have done a very similar thing in the past. I split the search string on the space character and restricted it to a maximum of 6 search "words", so that the linq is still manageable.
I came up with something like the following: