Linq-to-SQL语句问题 [英] Linq-to-SQL statement issue

查看:66
本文介绍了Linq-to-SQL语句问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上希望将搜索查询绑定到一个不错的gridview上,但这必须由用户输入查询来完成(有点像搜索功能).我可以得到单个值和返回的行,但是如何在数据库中的所有列中搜索输入的值并返回它呢?

I am basically looking to bind a search query to a gridview which is nice, but this must be done by a users input query (sort of like a search function). I can get single values and rows returned, but how would I get it to search all columns in my database for the inputted values and return it?

到目前为止,我的代码是:

My code so far is:

Void SearchFunction()
{
  TiamoDataContext context = new TiamoDataContext();

  var search from p in context.UserProfiles
      where p.DanceType == UserSearchString
      select p;

  UserSearchGrid.DataSource = search;
  UserSearchGrid.DataBind();
}

我尝试过p.equals,但可以肯定的是这不是解决问题的方法.

I tried p.equals but am pretty sure thats not the way to go about it.

推荐答案

如果希望它搜索表中的每一列,则必须告诉它搜索表中的每一列.

If you want it to search every column in the table, then you have to tell it to search every column in the table.

var search =
    from p in context.UserProfiles
    where 
        p.DanceType == UserSearchString ||
        p.Foo == UserSearchString ||
        p.Bar == UserSearchString
    select p;

仅此而已.没有魔术运算符会自动执行此操作(不可能-有些列甚至可能不是字符串).

That's all there is to it. There's no magic operator that will do it automatically (there can't be - some columns might not even be strings).

请记住,这可能会非常慢,因为查询优化器将无法选择可以处理整个查询的单个索引.

Keep in mind that this will likely be very slow, as the query optimizer won't be able to pick a single index that can handle this entire query.

顺便说一句,此搜索"仅测试纯相等性.您可能想分别使用StartsWithContains进行前缀或子字符串搜索.

As an aside, this "search" only tests for pure equality. You might want to use StartsWith or Contains for a prefix or substring search, respectively.

这篇关于Linq-to-SQL语句问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆