I am trying to implement a search function where the results could be populated to a Gridview. I am using LINQ to query my db in c#.
The problem I am trying to figure out is, if the user want to search through multiple columns/fields with more than one search term, then how do I do that? So instance if I search a table with these 3 fields and I had some data like: > firstName | lastname | players# >>1. Michael | Jordan | 12 >>2. Michael | Jordan | 24 >>3. Michael | Jordan | 45 >>4. DeAndre| Jordan | 6 >>5. Jerome| Jordan | 44 >>6. Jordan| Sparks| 88
Now if I search for Jordan I get everything:
If I search Michael Jordan than I should get row #s 1,2,3 back.
If I search Jordan Sparks 88 than I should row # 6 back.
So my problem is I don't know where in the table the search term may be so I must search all columns/fields. In my current code, I have something where I go through each column/field name and use the Contains() then || ("or"), but it only works for 1 search term.
Is there an elegant and easy way to search and filter through a whole linq table so that the list is minimized? From there I will add that result to a datasource and then bind it.
@Sphinxxx I am using a typical gridview. It looks like this:
<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>
My current search function looks something like this:
/**
* 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
}