gpt4 book ai didi

c# - 使用 LINQ 进行全文搜索

转载 作者:太空狗 更新时间:2023-10-30 00:53:13 27 4
gpt4 key购买 nike

我正在尝试实现一个搜索功能,可以将结果填充到 Gridview。我正在使用 LINQ 在 C# 中查询我的数据库。

我想弄清楚的问题是,如果用户想要使用多个搜索词搜索多个列/字段,那么我该怎么做呢?例如,如果我用这 3 个字段搜索一个表,并且我有一些数据,例如:> 名字 |姓 |球员#>>1.迈克尔 |乔丹 | 12>>2。迈克尔 |乔丹 | 24>>3。迈克尔 |乔丹 | 45>>4。德安德鲁|乔丹 | 6个>>5。杰罗姆|乔丹 | 44>>6。约旦| Spark | 88

现在,如果我搜索 Jordan,我会得到所有内容:

如果我搜索 Michael Jordan,那么我应该返回第 1、2、3 行。

如果我搜索 Jordan Sparks 88,我应该返回第 6 行。

所以我的问题是我不知道搜索词在表中的什么位置,所以我必须搜索所有列/字段。在我当前的代码中,我有一些东西可以遍历每个列/字段名称并使用 Contains() 然后 || (“或”),但它仅适用于 1 个搜索词。

是否有一种优雅而简单的方法来搜索和过滤整个 linq 表,以便将列表最小化?我将从那里将该结果添加到数据源,然后绑定(bind)它。

@Sphinxxx 我使用的是典型的 GridView 。它看起来像这样:

    <asp:GridView ID="GridView" runat="server" AllowSorting="True" PagerStyle-Mode="NumericPages"
AutoGenerateColumns="false" Width="100%" CssClass="gridView" OnPageIndexChanging="GridView_PageIndexChanging"
AllowPaging="True" DataKeyNames="idPlayersList" OnRowCommand="GridView_RowCommand"
OnRowEditing="GridView_RowEditing" OnRowCancelingEdit="GridView_CancelEditRow"
OnRowUpdating="GridView_UpdateRow" OnRowDataBound="GridView_RowDataBound">
<RowStyle CssClass="rowStyle"></RowStyle>
<asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
<asp:BoundField DataField="PlayersNumber" HeaderText="Players Number" SortExpression="PlayersNumber" />
<asp:TemplateField HeaderText="Team" SortExpression="Team">
<EditItemTemplate>
<asp:DropDownList ID="ddlTeam" runat="server" CssClass="dropdown" AutoPostBack="True"
AppendDataBoundItems="true" DataTextField="TeamName" DataValueField="idTeam">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTeam" runat="server" Text='<%# Bind("TeamName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

我当前的搜索功能如下所示:

/**
* This method is for button search functionality
*
*/
protected void btnSearch_Click(object sender, EventArgs e)
{

// Call to Entity Model Framework
DBModel.DBEntities context = new DBModel.DBEntities();

string[] searchTerms = txtSearch.Text.Trim().Split('&');

//Prepare to build a "players" query:
IQueryable<DBModel.playersList> playersListQuery = context.playersLists;

foreach (var term in searchTerms)
{
//Refine our query, one search term at a time:
playersListQuery = playersListQuery.Where(p => p.isDeleted == false && (p.FirstName.Contains(term.Trim()) ||
p.LastName.Contains(term.Trim()) ||
p.PlayersNumber.Contains(term.Trim()) ||
p.Team.Name.Contains(term.Trim())));
}

//Now we have the complete query. Get the results from the database:
var filteredplayersList = playersListQuery.Select(s => new
{

idPlayersList = s.idPlayersList,
FirstName = s.FirstName,
LastName = s.LastName,
PlayersNumber = s.PlayersNumber,
TeamName = s.Team.Name
}).ToList();


GridView.DataSource = filteredplayersList; //Connecting query to the datasource Gridview
GridView.DataBind(); //Binding Gridview
}

最佳答案

幸运的是,在 Linq2SQL 中创建多个 AND(每个搜索词一个)很容易 - 只需继续添加 .Where(.. 到您的查询:

string[] searchTerms = "Jordan Sparks 88".Split(' ');

using (var dc = new MyDataContext())
{
//Using DataContext.Log is handy
//if we want to look at Linq2SQL's generated SQL:
dc.Log = new System.IO.StringWriter();

//Prepare to build a "players" query:
IQueryable<Player> playersQuery = dc.Players;

//Refine our query, one search term at a time:
foreach (var term in searchTerms)
{
//Create (and use) a local variable of the search term
//to avoid the "outer variable trap":
//http://stackoverflow.com/questions/3416758
//http://stackoverflow.com/questions/295593
var currentTerm = term.Trim();
playersQuery = playersQuery.Where(p => (p.isDeleted == false)
&&
(p.FirstName.Contains(currentTerm) ||
p.LastName.Contains(currentTerm) ||
p.PlayersNumber.Contains(currentTerm) ||
p.Team.Name.Contains(currentTerm))
);
}

//Now we have the complete query. Get the results from the database:
var filteredPlayers = playersQuery.Select(p => new
{
p.idPlayer,
p.FirstName,
p.LastName,
p.PlayersNumber,
TeamName = p.Team.Name
})
.ToArray();

//See if the generated SQL looked like it was supposed to:
var sql = dc.Log.ToString();
}

关于c# - 使用 LINQ 进行全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17414907/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com