返回使用LINQ和多个“关键字”的结果吗? [英] Returning results using LINQ and multiple 'keywords'?

查看:109
本文介绍了返回使用LINQ和多个“关键字”的结果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的ASP.Net和LINQ。我有一个小项目,我的工作。它基本上包括屏幕的四个文本框,一个ListView控件和一个数据库表中的搜索按钮。

I'm new to ASP.Net and LINQ. I have a small project I'm working on. It basically consists of a screen with four text boxes, a listview control and a search button with one database table.

每个文本框中重新presents某一个领域:作者,标题,出版商和价格。我设想的是,用户会在一个或更多的字段输入文本并点击搜索按钮。然后程序将返回任何结果可以发现符合用户的标准的。

Each text box represents a certain field: Author, Title, Publisher, and Price. What I envision is that a user would input text in one, or more, of the fields and hit the search button. The program would then return whatever results could be found that match the user's criteria.

如果我使用了一个SQL语句,我只是选择每个匹配任何输入字段的记录(即SELECT作者,书名,出版社,价格从书本WHERE ...)。不过,我不太清楚如何使用LINQ做到这一点。

If I were using an SQL statement, I'd just select every record that matches any of the input fields (i.e. SELECT author, title, publisher, price FROM books WHERE...). However, I'm not quite sure how to do this with LINQ.

因此​​,没有人对我的出发点?我见过LINQ的例子有一个字段作为搜索限制器:

So, does anyone have a starting point for me? I've seen LINQ examples with one field as a limiter on the search:

public void SimpleSearch()
{
    DataClasses1DataContext dc = new DataClasses1DataContext();

    var q =
        from a in dc.GetTable<Books>()
        where a.Title == "1984"
        select a;

    dataGridView1.DataSource = q;
}

但是我似乎无法找到使用一个以上的限制器上的搜索的任何其他实施例。我开始认为这是不可能的。如果是这样,有人可以推荐不同的方式为我完成我想要做什么?基本上,我只是想搜索匹配用户的输入,表格中的表和列表视图返回结果。任何帮助将大大AP preciate。

But I can't seem to find any other examples that use more than one limiter on the search. I'm beginning to think it isn't possible. If so, can someone recommend a different way for me to accomplish what I'm trying to do? Basically, I just want to search the table for fields that match the user's input and return the results in a listview. Any help would be greatly appreciate.

推荐答案

我喜欢用含有使搜索多一点模糊的,也是我喜欢设置一切为小写,所以没有执行搜索时区分大小写的问题

I like to use contains to make the search a little more fuzzy and also I like to set everything to lowercase so there is no case sensitivity issues when performing the search.

public void SimpleSearch()
{
  DataClasses1DataContext dc = new DataClasses1DataContext();

  var search = txtSearch.Text.ToLower();

var q =
    from a in dc.GetTable<Books>()
    where a.Title.ToLower() == search ||
    a.Author.ToLower() == search ||
    a.Author.ToLower().Contains(search) ||
    a.Title.ToLower().Contains(search)
    select a;

dataGridView1.DataSource = q;
}

这篇关于返回使用LINQ和多个“关键字”的结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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