Linq to sql 查询多列 - 查询帮助

发布于 2024-12-03 17:43:42 字数 826 浏览 1 评论 0原文

我的屏幕上有一个文本字段,用户可以从一个输入字段中搜索一些内容:

  1. 地址
  2. 邮政编码
  3. 客户名称
  4. 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:

  1. Address
  2. Postcode
  3. Client name
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单 2024-12-10 17:43:42

也许一个想法:在数据库中创建一个额外的列,其中包含您想要搜索的所有列作为一个大的串联字符串。然后对输入字符串与数据库中的该字段进行自由文本搜索。

由于 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)

秉烛思 2024-12-10 17:43:42

哎呀 - 有没有办法将输入拆分为 ID、地址、邮政编码和姓名的单独字段?

如果是这样,您可以继续附加 Where 子句,例如

var query = filteredList;
if (!String.IsNullOrEmpty(id))
{
    query = query.Where(c => c.ID.Contains(id))
}
if (!String.IsNullOrEmpty(name))
{
    query = query.Where(c => c.Name.Contains(name))
}

.. 与名称、地址等相同。

否则,您的查询更像是搜索引擎查询,而不是 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

var query = filteredList;
if (!String.IsNullOrEmpty(id))
{
    query = query.Where(c => c.ID.Contains(id))
}
if (!String.IsNullOrEmpty(name))
{
    query = query.Where(c => c.Name.Contains(name))
}

.. same for Name, address etc

Otherwise, your query resembles more of a search engine query than a RDBMS query.

简美 2024-12-10 17:43:42

我过去也做过类似的事情。我将搜索字符串拆分为空格字符,并将其限制为最多 6 个搜索“单词”,以便 linq 仍然易于管理。

我想出了如下的方法:

string[] words = q.ToLower().Split(' ');
string[] wordsFixed = new string[] {"", "", "", "", "", "" };

for(int i = 0; i < 6 && i < words.Length; i++)
    wordsFixed[i] = words[i];

    var data = from item in list
                    where (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[0]) &&
                          (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[1]) &&
                          (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[2]) &&
                          (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[3]) &&
                          (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[4]) &&
                          (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[5])
                    select item;

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:

string[] words = q.ToLower().Split(' ');
string[] wordsFixed = new string[] {"", "", "", "", "", "" };

for(int i = 0; i < 6 && i < words.Length; i++)
    wordsFixed[i] = words[i];

    var data = from item in list
                    where (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[0]) &&
                          (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[1]) &&
                          (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[2]) &&
                          (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[3]) &&
                          (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[4]) &&
                          (item.ID + item.Name + item.Address1 + item.Address2 + item.PostCode).ToLower().Contains(wordsFixed[5])
                    select item;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文